Cod sursa(job #678332)
Utilizator | Data | 11 februarie 2012 15:02:32 | |
---|---|---|---|
Problema | Deque | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.62 kb |
#include<stdio.h>
#define Nmax 5000001
using namespace std;
int N,K,v[2][Nmax];
int main()
{
FILE*f = fopen("deque.in","r");
fscanf(f,"%d %d",&N,&K);
int i=1,pos = 0,x,st=1;
long long sum=0;
for(i=1;i<=N;++i)
{
fscanf(f,"%d ",&x);
while(pos>=st && v[0][pos] >= x)
--pos;
++pos;
v[0][pos] = x;
v[1][pos] = i;
if(i-K == v[1][st])
++st;
if(i>=K)
sum += v[0][st];
}
fclose(f);
FILE*g = fopen("deque.out","w");
fprintf(g,"%lld\n",sum);
fclose(g);
return 0;
}