Cod sursa(job #1962687)

Utilizator GandalfTheWhiteGandalf the White GandalfTheWhite Data 11 aprilie 2017 23:13:53
Problema Deque Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <cstdio>
#include <vector>
#include <deque>
#define N 5000005
using namespace std;

vector <int> A;
deque <int> Q;

int n,k,sum;

void Read(){

    int i,x;
    freopen("deque.in","r",stdin);

    scanf("%d%d",&n,&k);
    A.push_back(0);
    for (i=1;i<=n;++i){
        scanf("%d",&x);
        A.push_back(x);
        }
}

void Solve(){

    int i;

    for (i=1;i<=n;++i){

        while (!Q.empty() && A[i]<=A[Q.back()]) Q.pop_back();
        Q.push_back(i);
        if (Q.front()==i-k) Q.pop_front();
        if (i>=k) sum+=A[Q.front()];
        }
}

void Write(){

    freopen("deque.out","w",stdout);

    printf("%d",sum);
}
int main()
{
    Read();
    Solve();
    Write();
    return 0;
}