Cod sursa(job #458238)

Utilizator hendrikHendrik Lai hendrik Data 24 mai 2010 06:05:30
Problema Carnati Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define FORD(i,b,a) for (int i=(b);i>=(a);i--)
#define MAXN 2010
#define x first
#define y second
typedef pair<int,int>pii;
int n,c,lo,hi,T[MAXN],P[MAXN],num,next[MAXN],prev[MAXN],ans;
pii p[MAXN];
bool used[MAXN];

void open(){
	freopen("carnati.in","r",stdin);
	freopen("carnati.out","w",stdout);
}

int main(){
	open();
	scanf("%d%d",&n,&c);
	hi=0;
	lo=1000000;
	REP(i,n){
		scanf("%d%d",&p[i].x,&p[i].y);
	}
	sort(p,p+n);
	REP(i,n){
		lo=0;hi=n-1;
		while (p[lo].y<p[i].y) lo++;
		while (p[hi].y<p[i].y) hi--;
		FOR(j,lo,hi) used[j]=0;
		num=0;
		FOR(j,lo,hi){
			if (p[j].y>=p[i].y){
				used[j]=1;
				num++;
			}
			next[j]=prev[j]=-1;
		}
		int now;
		now=lo;
		FOR(j,lo+1,hi){
			if (used[j]){
				prev[j]=now;
				now=j;
			}
		}
		now=hi;
		FORD(j,hi-1,lo){
			if (used[j]){
				next[j]=now;
				now=j;
			}
		}
		int tmp,tmp2;
		tmp=num*p[i].y-c*(p[hi].x-p[lo].x+1);
		if (lo==hi){
			ans=max(ans,tmp);
			continue;
		}
		bool change=1;
		while (change){
			change=0;
			tmp2=(num-1)*p[i].y-c*(p[hi].x-p[next[lo]].x+1);
			if (tmp2>tmp){
				change=1;
				num--;
				tmp=tmp2;
				lo=next[lo];
			}
			tmp2=(num-1)*p[i].y-c*(p[prev[hi]].x-p[lo].x+1);
			if (tmp2>tmp){
				change=1;				
				num--;
				tmp=tmp2;
				hi=prev[hi];
			}
		}
		ans=max(ans,tmp);
	}
	printf("%d\n",ans);
	return 0;
}