Cod sursa(job #1708469)

Utilizator ionanghelinaIonut Anghelina ionanghelina Data 27 mai 2016 10:08:59
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include<cstdio>
#include<deque>
using namespace std;
deque<int> q1,q2;
int n,k,x;
 long long s;
int main()
{
    freopen("deque.in","r",stdin);
    freopen("deque.out","w",stdout);
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        while (!q2.empty() && q2.front()<=(i-k))
        {
            q1.pop_front();
            q2.pop_front();
        }
        while (!q1.empty() && q1.back()>x)
        {
            q1.pop_back();
            q2.pop_back();
        }
        q1.push_back(x);
        q2.push_back(i);
        if (i>=k) s=s+q1.front()*1LL;
    }
    printf("%lld\n",s);
    return 0;
}