Cod sursa(job #2655309)

Utilizator vladth11Vlad Haivas vladth11 Data 3 octombrie 2020 22:29:07
Problema Euro Scor 75
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.28 kb
///Multumiri sursei lui Dani pentru indrumare :)
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "

using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <ll, pii> muchie;

const ll NMAX = 34568;
const ll INF = (1 << 22) - 1;
const ll MOD = 1000000007;
const ll BLOCK = 101;

struct functie{
    int a, b;
    int valoare(int x){
        return a * x + b;
    }
};

class LiChao{
public:
    functie tree[NMAX * 4];
    void update(int node, int st, int dr, functie f){
        if(st == dr){
            if(tree[node].valoare(st) < f.valoare(st)){
                swap(tree[node], f);
            }
            return;
        }
        int mid = (st + dr) / 2;
        int inceput = (tree[node].valoare(st) < f.valoare(st));
        int mijloc = (tree[node].valoare(mid) < f.valoare(mid));
        if(inceput && mijloc){
            swap(tree[node], f);
            update(node * 2 + 1, mid + 1, dr, f);
        }
        if(inceput && !mijloc){
            update(node * 2, st, mid, f);
        }
        if(!inceput && mijloc){
            swap(tree[node], f);
            update(node * 2, st, mid, f);
        }
        if(!inceput && !mijloc){
            update(node * 2 + 1, mid + 1, dr, f);
        }
    }
    int query(int node, int st, int dr, int poz){
         if(st == dr){
            return tree[node].valoare(poz);
         }
         int mid = (st + dr) / 2;
         if(poz <= mid){
            return max(query(node * 2, st, mid, poz), tree[node].valoare(poz));
         }else{
            return max(query(node * 2 + 1, mid + 1, dr, poz), tree[node].valoare(poz));
         }
    }
}lichao;

int s[NMAX];

int main() {
    ifstream cin("euro.in");
    ofstream cout("euro.out");
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,i,t;
    cin >> n >> t;
    for(i = 1;i <= n;i++){
        int x;
        cin >> x;
        s[i] = s[i - 1] + x;
    }
    for(i = 1;i <= n;i++){
        int best = lichao.query(1, 1, n, i) - t + s[i] * i;
        if(i == n){
            cout << best;
            return 0;
        }
        lichao.update(1, 1, n, {-s[i], best});
    }
    return 0;
}