Cod sursa(job #2060787)

Utilizator anca.sotirAnca Sotir anca.sotir Data 8 noiembrie 2017 18:35:51
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>
#define nmax 5000000
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
long long int top,bottom=1,s,N,K;
struct element{
    long long int val;
    int poz;
    } D[nmax];
void push(long long int x)
{
    D[++top].val=x;
}
void pop_top()
{
    if(top>0)
        --top;
}
void pop_bottom()
{
    ++bottom;
}
int main()
{
    f>>N>>K;
    for(int i=1;i<=N;i++)
    {
        long long int x;
        f>>x;
        if(i-D[bottom].poz==K)
            pop_bottom();
        while(x<D[top].val&&top>=bottom)
                pop_top();
        push(x);
        D[top].poz=i;
        if(i>=K)
            s+=D[bottom].val;
    }
    g<<s;
    return 0;
}