Cod sursa(job #1259089)

Utilizator VisanCosminVisan Tudor Cosmin VisanCosmin Data 9 noiembrie 2014 18:15:08
Problema Carnati Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <stdio.h>
#include <algorithm>

long long n;
long long p,old,maxs,news;

struct str
{
    long c,t;
}v[2001];


bool cmp(const str &a,const str&b)
{
    return a.t<b.t;
}

int main()
{
    FILE*f =fopen("carnati.in","r");
    fscanf(f,"%d%d",&n,&p);
    for(int i = 0 ;i<n;i++)
        fscanf(f,"%d%d",&v[i].t,&v[i].c);
    fclose(f);

    std::sort(v,v+n+1,cmp);

    long y = 0;
    for(int i = 0 ;i<n;i++)
    {
        old = news = 0;

        for(int j = 0 ;j<n;j++)
        {
            if(v[j].c>=v[i].c) y = v[i].c;
            else y = 0;

            news = old -(v[j].t-v[j-1].t)*p+y;
            if(news < y-p) news = y-p;
            if(news >= maxs) maxs = news;
            old = news;
        }
    }

    FILE * g = fopen("carnati.out","w");
    fprintf(g,"%d",maxs);
    fclose(g);
    return 0;
}