Cod sursa(job #1220230)

Utilizator andreas.chelsauAndreas Chelsau andreas.chelsau Data 16 august 2014 21:50:13
Problema Carnati Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

struct pk{
	int time,gain;
};
int n,c;
pk a[2005];

void solve(){
	int localMax = -c;
	int newJ,max = -(1 << 30);
	for(int i = 0; i < n; i++){
		newJ = -1;
		localMax = -c;
		for(int j = 0; j < n; j++){
			if(a[i].gain <= a[j].gain){
				int idx = newJ == -1 ? j : newJ;
				int g = a[i].gain - abs(a[j].time - a[idx].time) * c;
				if(localMax < localMax + g){
					localMax += g;
					newJ = j;
				}
				else{
					localMax = -c;
					newJ = -1;
				}
				if(localMax > max)
					max = localMax;

				/*rez[i] = MAX(rez[i],rez[i] + c + a[i].gain - (abs(a[j].time - a[i].time) + 1) * c);	
				if(j > i)*/
			}
		}
	}
	printf("%d\n",max);

}
int cmp(pk a, pk b)
{
    return a.time < b.time;
}
int main(){
	freopen("carnati.in","r",stdin);
	freopen("carnati.out","w",stdout);

	scanf("%d%d",&n,&c);
	int g,t;
	for(int i = 0; i < n; i++){
		scanf("%d%d",&t,&g);
		a[i].gain = g;
		a[i].time = t;
	}
	sort(a,a+n,cmp);
	solve();

	return 0;
}