Cod sursa(job #1528434)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 19 noiembrie 2015 18:18:38
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;

deque < pair<int, int> > d;

int main()
{
    int n,k,x,i;
    freopen("deque.in", "r", stdin);
    freopen("deque.out", "w", stdout);
    scanf("%d %d",&n,&k);
    scanf("%d",&x);
    d.pb(mp(x,1));
    unsigned long long s = 0;
    for(i = 2;i <= n;i++){
        scanf("%d",&x);
        while(d.front().second <= i-k && d.size()){
            d.pop_front();
        }
        if(x <= d.front().first){
            d.clear();
            d.pb(mp(x,i));
        }else{
            while((d.back().second <= i-k || d.back().first > x) && d.size()){
                d.pop_back();
            }
            d.pb(mp(x,i));
        }
        if(i >= k){
            s += d.front().first;
        }
    }
    printf("%lld",s);
    return 0;
}