Cod sursa(job #1346249)

Utilizator MihailPJack ONeill MihailP Data 18 februarie 2015 09:16:19
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <stdio.h>
#include <algorithm>

long long n;
long long p, old, maxs, news;

struct str
{
	long c, t;
}v[2001];


bool cmp(const str &a, const str&b)
{
	return a.t<b.t;
}

int main()
{
	FILE*f = fopen("carnati.in", "r");
	fscanf(f, "%d%d", &n, &p);
	for (int i = 1; i <= n; i++)
		fscanf(f, "%d%d", &v[i].t, &v[i].c);
	fclose(f);

	v[0].t = v[0].c = -10;
	std::sort(v + 1, v + n + 1, cmp);

	long y = 0;
	for (int i = 1; i <= n; i++)
	{
		news = 0;

		for (int j = 1; j <= n; j++)
		{
			if (v[j].c >= v[i].c) y = v[i].c;
			else y = 0;

			news = news - (v[j].t - v[j - 1].t)*p + y;
			if (news < y - p) news = y - p;
			if (news >= maxs) maxs = news;
			
		}
	}

	FILE * g = fopen("carnati.out", "w");
	fprintf(g, "%d", maxs);
	fclose(g);
	return 0;
}