Cod sursa(job #1777735)

Utilizator silkMarin Dragos silk Data 12 octombrie 2016 20:41:11
Problema Carnati Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <cstdio>
#include <algorithm>
#define INF 1<<30
#define NMax 2000
#define VMax 1500
#define mp make_pair
using namespace std;

pair<int, int> v[NMax+1];
int aux[NMax+1];

int main(){
    freopen("carnati.in","r",stdin);
    freopen("carnati.out","w",stdout);

    int i,j,N,C,T,x,y,ans,temp,k,t,timp;

    scanf("%d %d",&N,&C);
    for(i = 1; i <= N; ++i)
    {
        scanf("%d %d",&x,&y);
        v[i] = mp(x,y);
    }

    sort(v+1,v+N+1);
    for(ans = -INF, T = 1; T <= VMax; ++T)
    {
        for(i = j = 1; j <= N && v[j].first - v[1].first + 1 <= T; ++j);
        --j;

        while(j <= N)
        {
            for(k = i; k <= j; ++k) aux[k-i+1] = v[k].second;
            t = j-i+1;
            timp = v[j].first - v[i].first + 1;
            sort(aux+1,aux+t+1);

            for(k = 1; k <= t; ++k)
            {
                temp = aux[k] * (t-k+1) - timp * C;
                if( ans < temp ) ans = temp;
            }

            for(++j; v[j].first == v[j+1].first; ++j);
            for(; v[j].first - v[i].first + 1 > T; ++i);
        }
    }

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




return 0;
}