Cod sursa(job #1644719)

Utilizator alex_topTop Alexandru alex_top Data 10 martie 2016 08:53:14
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <fstream>
#include <deque>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
struct nod
{
    int a,b;
};
deque <nod>q;
int n,k,i,x;
long long s;
int main()
{
    f>>n>>k;
    nod p;
    for(i=1;i<=n;i++)
    {
        f>>x;
        if(!q.empty())
        {
            while(q.back().b>=x)
            {
                q.pop_back();
                if(q.empty()) break;
            }
        }
        if(!q.empty())
        {
            if(q.front().a<i-k+1) q.pop_front();

        }
        p.a=i;
        p.b=x;
        q.push_back(p);
        if(i>=k)
            s=s+q.front().b;
    }
    g<<s;
    return 0;
}