Cod sursa(job #1778312)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 13 octombrie 2016 18:06:19
Problema Deque Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

ifstream f ("deque.in");
ofstream t ("deque.out");

deque <pair<int,int> > v;
int32_t n,k;

inline void clean_back(int32_t x)
{
    while (v.back().first>x)
        v.pop_back();
}

inline void clean_front(int32_t cur_date)
{
    while (cur_date-v.front().second>=k)
        v.pop_front();
}

int main()
{
    int64_t x,s=0;
    f>>n>>k;
    f>>x;
    v.push_front(make_pair(x,0));
    for (int64_t i=1; i<k; ++i)
    {
        f>>x;
        if (x<v[0].first)
            v.push_front(make_pair(x,i));
        else
            clean_back(x);
        v.push_back(make_pair(x,i));
    }
    s+=v[0].first;
    for (int64_t i=k; i<n; ++i)
    {
        f>>x;
        if (x<v[0].first)
            v.push_front(make_pair(x,i));
        else
            clean_back(x);
        v.push_back(make_pair(x,i));
        clean_front(i);
        s+=v[0].first;
    }
    t<<s;
    return 0;
}