Cod sursa(job #1404516)

Utilizator ValentinSavoiuFMI Savoiu Valentin-Marian ValentinSavoiu Data 28 martie 2015 12:21:20
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <iostream>
#include <fstream>
#include <cstdio>
#define nmax 5000001
using namespace std;
ofstream g("deque.out");
long long dq[nmax],z,s,i,p;
int dq2[nmax],n,k,x;
int main()
{
    freopen("deque.in","r",stdin);
    scanf("%d%d",&n,&k);
    z=0;
    s=0;
    for(i=1;i<=k;i++)
    {
        scanf("%d",&x);
        while(x<dq[z]&&z>0)
        {
            z--;
        }
        z++;
        dq[z]=x;
        dq2[z]=i;
    }
    //g<<dq[1]<<'\n';
    s+=dq[1];
    p=1;
    for(i=k+1;i<=n;i++)
    {
        if(dq2[p]<=i-k) p++;
        scanf("%d",&x);
        while(x<dq[z]&&z>=p)
        {
            z--;
        }
        z++;
        dq[z]=x;
        dq2[z]=i;
        s+=1LL*dq[p];
        //g<<dq[p]<<'\n';
    }
    g<<s;
    return 0;
}