Cod sursa(job #2724341)

Utilizator PopescuMihneaPopescu Mihnea-Valentin PopescuMihnea Data 16 martie 2021 22:46:24
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ("deque.in");
ofstream g ("deque.out");
int coada[5000000],poz[5000000];
int main()
{
    int N,K,x,first,last;
    first=0;
    last=0;
    long long sol=0;
    f>>N>>K;
    for (int i=1; i<=N; i++)
    {
        f>>x;
        while (poz[first]<=i-K && first+1<=last)
            first++;
        while (coada[last-1]>=x && last-1>=first)
            last--;
        coada[last]=x;
        poz[last++]=i;
        if (i-K>=0)
            sol+=coada[first];
    }
    g<<sol;
}