Cod sursa(job #2041218)

Utilizator AnduRazvanMindrescu Andu AnduRazvan Data 16 octombrie 2017 22:39:16
Problema Deque Scor 25
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
struct nod
{ int val;
  int poz;
};
nod st[5000001];
int n,k,x,top,pr,sum;
int main()
{ fin>>n>>k;
int i;
fin>>x;
pr=1;
top=1;
st[1].val=x;
st[1].poz=1;
 for(i=2;i<=n;i++)
   {fin>>x;
    if(i-st[pr].poz+1>k) pr++;
    while(x<st[top].val&&top>=pr) top--;
    top++;
    st[top].val=x;
    st[top].poz=i;
    if(i>=k) sum+=st[pr].val;
   }
   fout<<sum;
    return 0;
}