Cod sursa(job #2239052)

Utilizator Mihai_PredaPreda Mihai Dragos Mihai_Preda Data 8 septembrie 2018 19:26:05
Problema Avioane Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

const long double INF = 1e9;

struct dreapta
{
//a*x + b
    dreapta(long long a = 0, long long b = 0, long long ind = -1)
    {
        this->a = a;
        this->b = b;
        this->ind = ind;
    }
    long long a, b, ind;
};

inline long double IntersectX(const dreapta &x, const dreapta &y)
{
    return 1.0 * (y.b - x.b) / (x.a - y.a);
}

int main()
{
    ifstream in("avioane.in");
    int n;
    in >> n;
    vector<long long> v(n);
    for(auto &x:v)
        in >> x;
    in.close();

    sort(v.begin(), v.end());
    vector<pair<long double, dreapta> > st;
    dreapta dr;
    int j = 0;
    long long rasp = 0;
    for(int i = 0; i < n; ++i)
    {
        while(j+1 < st.size() && st[j+1].first < i)
            j++;
        if(i > 0)
        {
            int ind = st[j].second.ind;
            rasp = max(rasp, 1LL * (n - i) * v[i] + 1LL * (i - ind) * v[ind]);
        }

        dr = dreapta(v[i], (n - i) * v[i] - n * v[i], i);
        while(st.size() > 1 && ( (dr.a == st.back().second.a && dr.b >= st.back().second.b) || IntersectX(dr, st.back().second) < st.back().first) )
        {
            st.pop_back();
        }
        if(st.empty())
            st.push_back(make_pair(-INF, dr));
        else
        {
            if(dr.a == st.back().second.a && dr.b < st.back().second.b)
                continue;
            st.push_back(make_pair(IntersectX(st.back().second, dr), dr));
        }

    }

    ofstream out("avioane.out");
    out << rasp;
    out.close();
    return 0;
}