Cod sursa(job #2930430)

Utilizator PatruMihaiPatru Mihai PatruMihai Data 28 octombrie 2022 13:56:55
Problema Buline Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    int n;
    fin >> n;

    vector<int> v(2 * n + 7);

    for(int i = 1; i <= n; i++)
    {
        int x, type;
        fin >> x >> type;

        v[i] = v[i + n] = x * (type == 0 ? -1 : 1);
    }

    deque<int> q;
    q.push_back(1);
    int smax = v[1];
    int pmax = 1;
    int lmax = 1;

    for(int i = 2; i <= 2 * n; i++)
    {
        v[i] += v[i - 1];

        while(!q.empty() && i - q.front() > n)
            q.pop_front();

        if(v[i] - v[q.front()] > smax)
        {
            smax = v[i] - v[q.front()];
            pmax = q.front() + 1;
            lmax = i - q.front();
        }

        while(!q.empty() && v[q.back()] > v[i])
            q.pop_back();

        q.push_back(i);
    }

    fout << smax << " " << pmax << " " << lmax;

    return 0;
}