Cod sursa(job #3211940)

Utilizator profinfo114Prof Info profinfo114 Data 10 martie 2024 20:12:45
Problema Buline Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <deque>

using namespace std;

ifstream fin("buline.in");
ofstream fout("buline.out");

int n, x, y, v[400005];
int smax, st, l, lmax;

int main() {
    fin >> n;
    for(int i = 1; i <= n; i++) {
        fin >> x >> y;
        if(y == 0) {
            v[i] -= x;
        } else {
            v[i] += x;
        }
        v[i + n] = v[i];
    }
    deque<int> DQ;
    DQ.push_back(1);
    for(int i = 2; i <= 2 * n; i++) {
        v[i] += v[i - 1];
        // eliminim toate elem care se repeta
        while(!DQ.empty() && i - DQ.front() > n) {
            DQ.pop_front();
        }
        if(v[i] - v[DQ.front()] > smax) {
            smax = v[i] - v[DQ.front()];
            st = DQ.front() + 1;
            lmax = i - DQ.front();
        }
        // scot elem. calculate inainte
        while(!DQ.empty() && v[DQ.back()] > v[i]) {
            DQ.pop_back();
        }
        DQ.push_back(i);
    }
    fout << smax << " " << st << " " << lmax;
    return 0;
}