Cod sursa(job #2610742)

Utilizator DeliaGhergheGherghe Ioana-Delia DeliaGherghe Data 5 mai 2020 16:35:42
Problema Deque Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
using namespace std;

int A[5000000], DQ[5000000], st = 0, dr = -1;
long long s;

void pushback(int x)
{
    dr++;
    DQ[dr] = x;
}

void popback()
{
    dr--;
}

void popfront()
{
    st++;
}

int main()
{
    ifstream fin("deque.in");
    ofstream fout("deque.out");
    int N,K,i;

    fin >> N >> K;
    for (i = 0; i < N; i++)
        fin >> A[i];

    for (i = 0; i < N; i++)
    {
        while (st <= dr && A[i] < A[DQ[dr]])
            popback();
        pushback(i);
        if (i - K == DQ[st])
            popfront();
        if (i >= K - 1)
                s = s + A[DQ[st]];
    }
    fout << s;
    fin.close();
    fout.close();

    return 0;
}