Cod sursa(job #1503251)

Utilizator daneel95Holteiu Daniel-Ninel daneel95 Data 15 octombrie 2015 19:38:23
Problema Deque Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
#include <deque>
#include <iostream>

using namespace std;

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

int vec[5000000];

int main()
{
    deque<int> a,b;
    int i,n,k,j;
    long long int s=0;
    in>>n;
    in>>k;
    for(i=0;i<n;i++) in>>vec[i];

    a.push_back(vec[0]);
    b.push_back(0);
    for(i=1;i<k;i++)
    {
        while(vec[i] < a[a.size()-1])
        {
            a.pop_back();
            b.pop_back();
        }
        a.push_back(vec[i]);
        b.push_back(i);
    }
    s+=a[0];
    for(i=k;i<n;i++)
    {
        while(vec[i] < a[a.size()-1])
        {
            a.pop_back();
            b.pop_back();
        }
        a.push_back(vec[i]);
        b.push_back(i);

        if(b[a.size()-1]-b[0]+1>k)
        {
            a.pop_front();
            b.pop_front();
        }

        s+=a[0];
    }
    out<<s;
    in.close();
    out.close();
    return 0;
}