Cod sursa(job #953766)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 27 mai 2013 11:24:44
Problema Avioane Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <deque>
#include <iterator>
#include <algorithm>

#define MAXN 100001

using namespace std;

struct Profit
{
    Profit() :
        Position(0),
        Start(0),
        End(0)
    {}

    Profit(int p, int s, int e) :
        Position(p),
        Start(s),
        End(e)
    {}

    int Position;
    int Start;
    int End;
};
 
long long profits[MAXN];
 
int main()
{
    int n;
    vector<int> vCustomers;
    fstream fin("avioane.in", fstream::in);
    fstream fout("avioane.out", fstream::out);

    fin >> n;

    istream_iterator<int> itIn(fin);
    copy_n(itIn, n, back_inserter(vCustomers));

    sort(vCustomers.begin(), vCustomers.end());

    deque<Profit> deq;
    deq.push_back(Profit(0, 0, n));

    profits[0] = vCustomers[0];

    long long MaxProfit = vCustomers[0];
    int MinEnd = n;
    for (int i=1; i<n; ++i)
    {
        if (vCustomers[i] != vCustomers[i-1])
        {
            while (!deq.empty() &&
                   i + 1LL * vCustomers[deq.back().Position] * (i - deq.back().Position) / (vCustomers[i] - vCustomers[deq.back().Position]) <= deq.back().Start)
            {
                deq.pop_back();
            }

            int start = 0;
            if (!deq.empty())
            {
                start = 1LL * vCustomers[deq.back().Position] * (i - deq.back().Position) / (vCustomers[i] - vCustomers[deq.back().Position]);
                deq.back().End = i + start;
            }

            MinEnd = min(MinEnd, i + start);

            deq.push_back(Profit(i, i + start, n));
        }

        while (deq.front().Start < MinEnd && MinEnd == i)
        {
            deq.pop_front();
        }

        MinEnd = deq.front().End;

        MaxProfit = 1LL * vCustomers[deq.front().Position] * (i - deq.front().Position + 1);

        profits[i] = MaxProfit;
    }

    MaxProfit = 0;
    for (int i=1; i<=n; ++i)
    {   
        MaxProfit = max(MaxProfit, profits[i-1] + 1LL * vCustomers[i] * (n - i));
    }

    fout << MaxProfit << "\n";

    return 0;
}