Cod sursa(job #2037605)

Utilizator ivan.tudorIvan Tudor ivan.tudor Data 12 octombrie 2017 16:20:57
Problema Deque Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 0.49 kb
#include <iostream>
#include<cstdio>
using namespace std;
int d[5000001];
int v[5000001];
int main()
{
    int i,n,k,s=0,st=0,dr=0;
    FILE*fin,*fout;
    fin=fopen("deque.in","r");
    fout=fopen("deque.out","w");
    fscanf(fin,"%d%d",&n,&k);
    for(i=0;i<n;i++){
      fscanf(fin,"%d",&v[i]);
      if(d[st]==i-k)
        st++;
      while(st<=dr && v[i]<=v[d[dr]]){
        dr--;
      }
      d[++dr]=i;
      if(i>=k-1)
        s+=v[d[st]];
    }
    fprintf(fout,"%d",s);
    return 0;
}