Cod sursa(job #3042333)

Utilizator GFA03Gavrila Florin-Alexandru GFA03 Data 5 aprilie 2023 20:39:40
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <deque>

int main()
{
    std::ifstream f1("deque.in");
    std::ofstream f2("deque.out");
    int n, k, x, S = 0, cnt = 0, cntStergeri = 0;
    bool turn = false;
    std::deque<int> poz;
    std::deque<int> rez;
    f1 >> n >> k;

    while(f1 >> x)
    {
        if(poz.size() > 0)
            if(rez.size() > k || cnt - k >= poz.front())
            {
                rez.pop_front();
                poz.pop_front();
            }
        if(rez.size() > 0)
            while(x < rez.back() && rez.size() > 0)
            {
                rez.pop_back();
                poz.pop_back();
            }
        rez.push_back(x);
        poz.push_back(cnt);
        if(cnt >= k-1)
        {
            S += rez.front();
        }
        cnt++;
    }
    f2 << S;
}