Cod sursa(job #968421)

Utilizator dropsdrop source drops Data 2 iulie 2013 00:31:33
Problema Coduri Huffman Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <ctime>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
using namespace std;
//ifstream ff("huffman.in");
//ofstream gg("huffman.out");
#define max 1000013

struct huf{ long long v;int k; struct huf *s,*d; 
	huf(long long v0=0,int k0=0){v=v0;s=d=0;k=k0;}
	huf(long long v0, huf *s0, huf *d0){v=v0;s=s0;d=d0;} }*h, *qq[max];
int n, ll[max];
long long vv[max], kk[max], p;

long long dfs(huf *r,int k,long long x){
    long long l=0;
    if(r->s==0 && r->d==0){
        vv[r->k]=x;
        ll[r->k]=k;
        return k*r->v;
    }
    if(r->s!=0) l+=dfs(r->s,k+1,2*x); 
    if(r->d!=0) l+=dfs(r->d,k+1,2*x+1);
    return l;
}

void arb(){
	huf *s,*d;
	int p, u, i;
	p=1; u=0; i=0;
	while((i<n || p<=u)){
		if(i==n || (p<=u && qq[p]->v<qq[i]->v))s=qq[p++]; else
			s=qq[i++];
		if(i==n || (p<=u && qq[p]->v<qq[i]->v))d=qq[p++]; else
			d=qq[i++];
			if(s==0||d==0){ break; }
			qq[++u]=new huf(s->v+d->v, s, d);
	}
	h=qq[u];
	//gg << dfs(h,0,0) << "\n";
	for(int i=0;i<n;i++) printf("%d %d\n", ll[i], vv[i]);
}

int main(){
	freopen("huffman.in","r",stdin);
	freopen("huffman.out","w",stdout);
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d", &vv[i]);
		qq[i]=new huf(vv[i],i);
	}
	arb();
}