Pagini recente » Cod sursa (job #2327514) | Cod sursa (job #907448) | Cod sursa (job #2799495) | Cod sursa (job #1607855) | Cod sursa (job #451221)
Cod sursa(job #451221)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on May 9, 2010, 11:43 AM
*/
#include <deque>
#include <cstdlib>
#include <fstream>
#define Nmax 5000011
#define SIZE 8219
/*
*
*/
using namespace std;
ifstream in;
int idx;
int v[Nmax];
char file[SIZE];
deque< int > dQ;
inline int read( int& x )
{
int sign=1;
while( file[idx] < '0' || file[idx] > '9' )
{
if( '-' == file[idx] )
sign=-1;
if( ++idx == SIZE )
{
idx=0;
in.read( file, SIZE );
}
}
for( x=0; file[idx] >= '0' && file[idx] <= '9'; )
{
x=x*10+file[idx]-'0';
if( ++idx == SIZE )
{
idx=0;
in.read( file, SIZE );
}
}
x*=sign;
}
int main(int argc, char** argv)
{
int i, N, k;
long long int s;
in.open( "deque.in" );
read(N); read(k);
for( i=1, s=0; i <= N; ++i )
{
read(v[i]);
for( ; !dQ.empty() && v[dQ.back()] >= v[i]; dQ.pop_back() );
dQ.push_back(i);
if( i >= k )
s+=v[dQ.front()];
if( i-dQ.front()+1 >= k )
dQ.pop_front();
}
ofstream out( "deque.out" );
out<<s<<'\n';
return (EXIT_SUCCESS);
}