Cod sursa(job #1101467)

Utilizator tac1234Tran Bach Nguyen tac1234 Data 8 februarie 2014 15:21:21
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <cstdio>
#include <deque>
using namespace std;
struct mystruct
{
    int x,y;
};
deque <mystruct> d;
inline void scoatere_elemente(int nr)
{
    while(!d.empty() && d.back().x>=nr)
        d.pop_back();
}
int main()
{
    freopen("deque.in","r",stdin);
    freopen("deque.out","w",stdout);
    int n,i,k,p,a;
    long long s=0;
    mystruct temp;
    scanf("%d%d",&n,&k);
    for(i=1; i<=n; ++i)
    {
        if (i<=k)
        {
            scanf("%d",&a);
            scoatere_elemente(a);
            temp.x=a;
            temp.y=i;
            d.push_back(temp);
        }
        if (i==k)
        {
            p=d.front().x;
            s+=p;
        }
        if (i>k)
        {
            scanf("%d",&a);
            if (i-k>=d.front().y && !d.empty())
                d.pop_front();
            scoatere_elemente(a);
            temp.x=a;
            temp.y=i;
            d.push_back(temp);
            p=d.front().x;
            s+=p;
        }
    }
    printf("%lld\n",s);
    return 0;
}