Cod sursa(job #2047479)

Utilizator anca.sotirAnca Sotir anca.sotir Data 24 octombrie 2017 21:15:23
Problema Deque Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#define nmax 5000000
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
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;
}