Cod sursa(job #1052960)

Utilizator alexsuciuAlex Suciu alexsuciu Data 11 decembrie 2013 22:46:36
Problema Deque Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <iostream>
#include<fstream>
#include<algorithm>

using namespace std;

ifstream f("deque.in");
ofstream g("deque.out");
int heap[5000005],poz[5000005],a[5000005],nr,k,aux;
int i,x,ki,n;

void urca(int pozi)
{
    while(pozi>1&& a[heap[pozi]]<a[heap[pozi>>1]])
    {
        //swap(heap[pozi],heap[pozi>>1]);
        aux=heap[pozi];
        heap[pozi]=heap[pozi>>1];
        heap[pozi>>1]=aux;
        poz[heap[pozi]]=pozi;
        poz[heap[pozi>>1]]=pozi>>1;
        pozi>>=1;

    }
}

void coboara(int pozi)
{
 {
    int poz1=0;

    while(pozi!=poz1) {
        poz1=pozi;

        if((poz1<<1)<=n && a[heap[pozi]]>a[heap[poz1<<1]])
            pozi=poz1<<1;
        if((poz1<<1)+1<=n && a[heap[pozi]]>a[heap[(poz1<<1)+1]])
            pozi=(poz1<<1)+1;

        //swap(heap[pozi],heap[poz1]);
        aux=heap[pozi];
        heap[pozi]=heap[poz1];
        heap[poz1]=aux;
        poz[heap[pozi]]=pozi;
        poz[heap[poz1]]=poz1;

        }
}}

int main()
{int ct=1;
long long sum=0;
    f>>nr>>ki;
    for(i=1;i<=ki;i++)
    {
        f>>x;
        a[++k]=x;
        heap[++n]=k;
        poz[k]=n;
        urca(n);
    }
    for(i=ki+1;i<=nr;i++)
    {
            sum+=a[heap[1]];
            a[ct]=12000000;
            heap[poz[ct]]=heap[n--];
            urca(poz[ct]);
            coboara(poz[ct]);
            f>>x;
            a[++k]=x;
            heap[++n]=k;
            poz[k]=n;
            urca(n);
            ct++;
        }
        g<<sum+a[heap[1]];
    }