Cod sursa(job #793806)

Utilizator Sm3USmeu Rares Sm3U Data 4 octombrie 2012 10:49:41
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <cstdio>
#include <queue>

using namespace std;

struct element{
    int nr;
    int poz;
    element (int x, int y){
        nr = x;
        poz = y;
    }
};

int n;
int k;
deque <element> a;
long long s;


void citire(){
    int x;

    freopen ("deque.in", "r", stdin);

    scanf ("%d", &n);
    scanf ("%d", &k);
    for (int i = 1; i < k; ++ i){
        scanf ("%d", &x);
        while (!a.empty() && a.back().nr >= x){
            a.pop_back();
        }
        a.push_back (element (x, i - 1));
    }
    for (int i = k - 1; i < n; ++ i){
        scanf ("%d", &x);
        if (a.front().poz <= i - k){
            a.pop_front();
        }
        while (!a.empty() && a.back().nr >= x){
            a.pop_back();
        }
        a.push_back (element (x, i));
        s += a.front().nr;
    }

}

void afisare(){
    freopen ("deque.out", "w", stdout);
    printf ("%lld", s);
}

int main()
{
    citire();
    afisare();

    return 0;
}