Cod sursa(job #777122)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 11 august 2012 01:57:31
Problema Carnati Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <algorithm>

#define MAX 2005

using namespace std;

struct client
{
    int p, t;
}v[MAX];

int dp[MAX], maxim, n, c;

bool cmp(client a, client b)
{
    return a.t < b.t;
}

int main()
{
    ifstream in("carnati.in"); in>>n>>c;
    for(int i = 1; i <= n; i++) in>>v[i].t>>v[i].p;
    in.close();
    sort(v + 1, v + n + 1, cmp);
    for(int i = 1; i <= n; i++)
    {
        int cost = v[i].p, g;
        for(int j = 1; j <= n; j++)
        {
            if(cost <= v[j].p)
                dp[j] = max(dp[j - 1] + cost - (v[j].t - v[j - 1].t) * c, cost - c);
            else
                dp[j] = max(0, dp[j - 1] - (v[j].t - v[j - 1].t) * c);
            maxim = max(maxim, dp[j]);
        }
    }
    ofstream out("carnati.out"); out<<maxim; out.close();
    return 0;
}