Cod sursa(job #970168)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 6 iulie 2013 07:19:24
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

#define MAXN 2001

using namespace std;

int prices[MAXN];
int sortedPrices[MAXN];
vector<int> times[MAXN];

int sums[MAXN];

int main()
{
    int n, C;
    int minTime = MAXN + 1;
    int maxTime = -1;
    fstream fin("carnati.in", fstream::in);
    fstream fout("carnati.out", fstream::out);
    
    fin >> n >> C;
    //cout << n << " " << C << endl;
    
    for (int i=1; i<=n; ++i)
    {
        int time;
        fin >> time >> prices[i];
        //cout << time << " " << prices[i] << endl;
        
        times[time].push_back(i);
        
        minTime = min(minTime, time);
        maxTime = max(maxTime, time);
        
        sortedPrices[i] = prices[i];
    }
    
    //cout << endl << minTime << " " << maxTime << endl;
    
    sort(sortedPrices + 1, sortedPrices + n + 1);
    
    /*ostream_iterator<int> itOut(cout, " ");
    copy_n(sortedPrices + 1, n, itOut);
    cout << endl;*/
    
    int maxProfit = - MAXN * C;
    
    for (int price=1; price<=n; ++price)
    {
        int minSum = 0;
        for (int t=0; t<=maxTime; ++t)
        {
            sums[t] = - C;

            if (t > 0)
            {
                sums[t] += sums[t-1];
            }

            for (int p : times[t])
            {
                if (prices[p] >= sortedPrices[price])
                {
                    sums[t] += sortedPrices[price];
                }
            }
            
            maxProfit = max(maxProfit, sums[t] - minSum);
            minSum = min(minSum, sums[t]);
        }

        /*copy_n(sums + minTime, maxTime + 1, itOut);
        cout << endl;
        cout << maxProfit << endl << endl;*/
    }
    
    fout << maxProfit << "\n";

    return 0;
}