Cod sursa(job #137211)

Utilizator bogdan2412Bogdan-Cristian Tataroiu bogdan2412 Data 17 februarie 2008 10:17:48
Problema Carnati Scor 0
Compilator cpp Status done
Runda preONI 2008, Runda 4, Clasa a 10-a Marime 1.23 kb
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int N, Cst;

#define MAXN 2048

vector<int> P;
vector< pair<int, int> > client;

int bst[MAXN][MAXN];
//bst[i][j] = profitul maxim pentru un interval [k, i], k <= i daca costul unui carnat este j (normalizat)

int main()
{
	freopen("carnati.in", "rt", stdin);
	freopen("carnati.out", "wt", stdout);

	scanf("%d %d", &N, &Cst);
	for (int i = 0; i < N; i++)
	{
		int cT, cP;
		scanf("%d %d", &cT, &cP);

		client.push_back( make_pair( cT, cP ) );
		P.push_back(cP);
	}

	sort( P.begin(), P.end() );
	P.resize( unique( P.begin(), P.end() ) - P.begin() );

	int Max = -1;
	for (int j = 0; j < (int)P.size(); j++)
	{
		if (client[0].second >= P[j])
			bst[0][j] = P[j] - Cst;
		else
			bst[0][j] = -Cst;
		if (bst[0][j] > Max)
			Max = bst[0][j];
	}

	for (int i = 1; i < N; i++)
		for (int j = 0; j < (int)P.size(); j++)
		{
			bst[i][j] = bst[i - 1][j] - (client[i].first - client[i - 1].first) * Cst;
			if (client[i].second >= P[j])
				bst[i][j] += P[j];

			//intervalul [i, i]
			int val = -Cst;
			if (client[i].second >= P[j])
				val += P[j];
			if (val > bst[i][j])
				bst[i][j] = val;
		
			if (bst[i][j] > Max)
				Max = bst[i][j];
		}

	printf("%d\n", Max);

	return 0;
}