Cod sursa(job #1032988)

Utilizator vladvaldezVlad Dimulescu vladvaldez Data 16 noiembrie 2013 11:49:09
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <stdio.h>

using namespace std;
FILE *f=fopen("deque.in","r");
FILE *g=fopen("deque.out","w");

int poz[5000005],h[5000005],v[5000005],i,n,nr,ct,x,k;
long long s;
void swapp(int a,int b)
{
 int aux;
 aux=h[a];
 h[a]=h[b];
 h[b]=aux;
 poz[h[a]]=a;
 poz[h[b]]=b;
}

void cern(int k,int n)
{
 int st=2*k;
 if(st<=n)
 {
  if (2*k+1<=n &&v[h[st+1]]<v[h[st]])st++;
  if  (v[h[k]]>v[h[st]]){swapp(k,st);cern(st,n);}
 }
}

void urc(int k)
{
  int t=k/2;
if(t>0)if (v[h[t]]>v[h[k]])
{
  swapp(t,k);
  urc(t);
}
}





int main()
{
fscanf(f,"%d%d",&n,&k);

for(i=1;i<=k;i++)
{
 fscanf(f,"%d",&v[i]);
 h[i]=i;
 poz[i]=i;
}
for(i=k/2;i>=1;i--)cern(i,k);

s+=v[h[1]];

for(i=k+1;i<=n;i++)
{
  fscanf(f,"%d",&v[i]);
  poz[i]=poz[i-k];
  h[poz[i]]=i;
  urc(poz[i]);
  cern(poz[i],k);
  s+=v[h[1]];
}


/*ct=1;
v[0]=-10000010;
for(i=1;i<=n;++i)
{
fscanf(f,"%d",&x);
v[i]=x;
if (i>k){cut(poz[ct]);ct++;}
insert();
if (i>=k)s+=v[h[1]]*1LL;
}
*/
fprintf(g,"%lld",s);

fclose(g);
return 0;
}