Cod sursa(job #2731188)

Utilizator FraNNNkieFrancesco-Gregorio Petrovici FraNNNkie Data 27 martie 2021 14:45:58
Problema Deque Scor 25
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
#define NMAX 5000005
using namespace std;
int v[NMAX], dq[NMAX];
int main()
{
    ifstream f("deque.in");
    ofstream g("deque.out");
    int frt, bck, n, k, i, sum = 0, j;
    f >> n >> k;
    for(i = 1; i <= n; ++i)
        f >> v[i];
    frt = 1;
    bck = 0;
    for(i = 1; i <= n; ++i)
    {
        while(frt <= bck && v[i] <= v[dq[bck]])
            bck --;
        dq[++ bck] = i;
        for(j = frt; j <= bck; j ++)
            cout << v[dq[j]] << " ";
        cout << endl;
        if(dq[frt] == i - k)
            frt ++;
        if(i >= k)
            sum += v[dq[frt]];
    }
    g << sum;
    return 0;
}