Cod sursa(job #2888539)

Utilizator SteanfaDiaconu Stefan Steanfa Data 11 aprilie 2022 15:49:37
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb

#include <fstream>
#include <iostream>
using namespace std;
class Asd
{
public:
    int first;
    int second;
};
Asd a[5000006];

int coada = 0, mar = 0, cap = 0;
void pushFront(const long long &x, const long long &c)
{
    cap++;
    a[cap].first = x;
    mar++;
    a[cap].second = c;
}
void popFront()
{
    mar--;
    cap--;
}
void popBack()
{
    coada++;
    mar--;
}
Asd back() { return a[coada +1]; }
bool gol() { return (!mar) ? 1 : 0; }
Asd front() { return a[cap]; }
int k = 0, n = 0, x;
long long mm = 0;

int main()
{

    ofstream cout("deque.out");
    ifstream cin("deque.in");
    // ifstream cin("dec.py");
    cin >> n >> k;

    for (size_t i = 0; i < n; i++)
    {
        cin >> x;
        while (!gol() and front().first > x)
        {
            popFront();
        }
        pushFront(x, i);

        if (!gol() and back().second == i - k)
        {
            popBack();
        }
        if (i >= -1 + k)
        {
            mm += back().first;
        }
    }

    cout << mm;
    return 0;
}