Cod sursa(job #1081219)

Utilizator NitaMihaitavoidcube NitaMihaita Data 13 ianuarie 2014 13:06:39
Problema Deque Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include<fstream>
#include<iostream>
#define numaru 5000000
using namespace std;
int hehe[numaru+1][2],dim_hehe;
void add_to_hehe(int x,int timp)
{
    hehe[++dim_hehe][0]=x;
    hehe[dim_hehe][1]=timp;
    int i=dim_hehe;
    while(i!=1 && hehe[i][0]<hehe[i/2][0])
    {
        hehe[i][0]=(hehe[i][0]+hehe[i/2][0])-(hehe[i/2][0]=hehe[i][0]);
        hehe[i][1]=(hehe[i][1]+hehe[i/2][1])-(hehe[i/2][1]=hehe[i][1]);
        i/=2;
    }
}
void da_l_afara()
{
    int i=1;
    hehe[1][0]=hehe[dim_hehe][0];hehe[1][1]=hehe[dim_hehe--][1];
    if(dim_hehe==0)return ;
    while( i*2 <=dim_hehe )
    {
        i=i*2;
        if(i+1<=dim_hehe && hehe[i+1][0]<hehe[i][0])++i;
        if( hehe[i][0]<hehe[i/2][0] )
        {
            hehe[i][0]=(hehe[i][0]+hehe[i/2][0])-(hehe[i/2][0]=hehe[i][0]);
            hehe[i][1]=(hehe[i][1]+hehe[i/2][1])-(hehe[i/2][1]=hehe[i][1]);
        }
        else break;
    }
}
void afishehe()
{
    for(int i=1;i<=dim_hehe;++i)cout<<"("<<hehe[i][0]<<", "<<hehe[i][1]<<") ";cout<<"\n";
}
int main()
{
    ifstream f("deque.in");
    ofstream g("deque.out");
    int n,i,k,x;
    long long sum=0;
    f>>n>>k;
    for(i=1;i<=n;++i)
    {
        f>>x;
        while( i-hehe[1][1]>=k ) da_l_afara();
        add_to_hehe(x,i);
        if(i>=k)
        {
            sum+=hehe[1][0];
        }
    }
    g<<sum<<"\n";

    f.close();
    g.close();
    return 0;
}