Cod sursa(job #1220146)

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

#define MAX(a,b) ((a) > (b) ? (a) : (b))

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

void solve(){
	for(int i = 0; i < n; i++)
		rez[i] = -c;
	int oldIdx;
	for(int i = 0; i < n; i++){
		oldIdx = i;
		for(int j = 0; j < n; j++){
			if(a[i].gain <= a[j].gain){
				if(i > j){
					oldIdx = oldIdx == -1 ? j : oldIdx;
					int g = a[i].gain - abs(a[j].time - a[oldIdx].time) * c;
					if(g > 0){
						rez[i] += g;
						oldIdx = - 1;
					}
					
				}
				else{
					oldIdx = oldIdx == -1 ? i : oldIdx;
					int g = a[i].gain - abs(a[j].time - a[oldIdx].time) * c;
					if(g > 0){
						rez[i] += g;
						oldIdx = j;
					}
					
				}
				/*rez[i] = MAX(rez[i],rez[i] + c + a[i].gain - (abs(a[j].time - a[i].time) + 1) * c);	
				if(j > i)*/
			}
		}
	}
	int max = -(1 << 30);
	for(int i = 0; i < n; i++)
		max = rez[i] > max ? rez[i] : max;
	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;
}