Cod sursa(job #2404775)

Utilizator theo2003Theodor Negrescu theo2003 Data 13 aprilie 2019 13:20:52
Problema Carnati Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <set>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("carnati.in");
ofstream out("carnati.out");
int n, c, maxp = 0;
pair<int, int> cust_array[2004], *cust = &cust_array[2];
int main() {
    in>>n>>c;
    cust[-2].first = -1000001;
    cust[-1].first = -1000000;
    cust[n + 1].first = 1000000;
    cust[n + 2].first = 1000001;
    for(int x = 0; x<n; x++) {
        in>>cust[x].first>>cust[x].second;
    }
    sort(cust, cust + n);
    for(int x = 0; x<n; x++) {
        int l = x, r = x, gainl = 0, gainr = 0, maxl = 0, maxr = 0, len = 1;
        while(cust[l - 1].first == cust[l].first) {
            l--;
            len += (cust[l].second >= cust[x].second);
        }
        while(cust[r].first == cust[r + 1].first) {
            r++;
            len += (cust[r].second >= cust[x].second);
        }
        for(int nl = l - 1, prev = l; nl>=0; nl--) {
            gainl -= c*(cust[prev].first - cust[nl].first);
            if(cust[nl].second >= cust[x].second)
                gainl += cust[x].second;
            maxl = max(maxl, gainl);
            prev = nl;
        }
        for(int nr = r + 1, prev = r; nr<n; nr++) {
            gainr -= c*(cust[nr].first - cust[prev].first);
            if(cust[nr].second >= cust[x].second)
                gainr += cust[x].second;
            maxr = max(maxr, gainr);
            prev = nr;
        }
        maxp = max(maxp, len*cust[x].second + maxl + maxr - c);
    }
    out<<maxp;
    return 0;
}