Mai intai trebuie sa te autentifici.
Cod sursa(job #1692374)
| Utilizator | Data | 20 aprilie 2016 19:12:03 | |
|---|---|---|---|
| Problema | Deque | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.64 kb |
#include <cstdio>
#define MAXN 5000000
int poz[MAXN+1],v[MAXN+1];
int main(){
FILE*fi,*fout;
int i,n,k,b,e;
long long s;
fi=fopen("deque.in" ,"r");
fout=fopen("deque.out" ,"w");
fscanf(fi,"%d%d" ,&n,&k);
for(i=1;i<=n;i++)
fscanf(fi,"%d" ,&v[i]);
b=0;
e=-1;
for(i=1;i<=k;i++){
while(e>=b&&v[poz[e]]>=v[i])
e--;
poz[++e]=i;
}
s=v[poz[b]];
for(i=k+1;i<=n;i++){
if(i-k>=poz[b])
b++;
while(e>=b&&v[poz[e]]>=v[i])
e--;
poz[++e]=i;
s=s+v[poz[b]];
}
fprintf(fout,"%lld" ,s);
fclose(fi);
fclose(fout);
return 0;
}
