Cod sursa(job #1125449)

Utilizator alexmarMarinescu Alexandru alexmar Data 26 februarie 2014 17:39:04
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <stdio.h>
#include<iostream>
#include<fstream>
#define maxn 5000010
 using namespace std;
int n,k;
int a[maxn], Deque[maxn];
int Front, Back;
long long Sum;

int main()
{
    ifstream fin("deque.in");
    ofstream fout("deque.out");

    int i;

    fin>>n>>k;

    Front=1;Back=0;
    for (i = 1; i <= n; i++)
        fin>>a[i];

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

            while(Front<=Back&&a[i]<=a[Deque[Back]])Back--;
            Deque[++Back]=i;

            if(Deque[Front]==i-k)Front++;
            if(i>=k) Sum+=a[Deque[Front]];

        }
fout<<Sum;
    return 0;
}