Cod sursa(job #460231)

Utilizator BitOneSAlexandru BitOne Data 1 iunie 2010 18:00:48
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <queue>
#include <cstdlib>
#include <fstream>
#define Nmax 5000011

/*
 *
 */
using namespace std;
int v[Nmax];
int main( void )
{
    int N, k, i;
    long long int s;
    deque< int > dQ;
    ifstream in( "deque.in" );
    in>>N>>k;
    for( s=0, i=1; i <= N; ++i )
    {
        in>>v[i];
        for( ; !dQ.empty() && v[i] <= v[dQ.back()]; dQ.pop_back() );
        dQ.push_back(i);
        if( i >= k )
            s+=v[dQ.front()];
        if( i-k+1 == dQ.front() )
          dQ.pop_front();
    }
    ofstream out( "deque.out" );
    out<<s<<'\n';
    return EXIT_SUCCESS;
}