Cod sursa(job #936723)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 8 aprilie 2013 15:41:15
Problema Coduri Huffman Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.88 kb
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#include <iomanip>
#include <time.h>
#include <list>
using namespace std;


const string file = "huffman";

const string infile = file + ".in";
const string outfile = file + ".out";


#define MAXN 1000003
#define INF 1LL << 62

struct HNod
{
	long long sum;
	int l;
	int r;

};

long long totalLg;
long long lg[MAXN];
long long codes[MAXN];

HNod nods[MAXN*2];
int N;
int alloc;


int q1[MAXN];
int q2[MAXN];

int l1;
int l2;
int r1;
int r2;


int extractMin()
{

	long long min1 = l1 != r1 ? nods[q1[l1]].sum : INF;
	long long min2 = l2 != r2 ? nods[q2[l2]].sum : INF;
	int result;
	if(min1 < min2)
	{
		result = q1[l1++];
	}
	else
	{
		result = q2[l2++];	
	}
	return result;
}


void citire()
{
	ifstream fin(infile.c_str());

	fin >> N;
	
	for(int i = 0; i < N; i++)
	{
		int x;
		fin >> x;
		nods[i].sum = x;

		q1[i] = i;
	}
	r1 = N;
	alloc = N;
	


	fin.close();
}



void DFS(int root, long long code, int level)
{
	if(root < N)
	{
		lg[root] = level;
		totalLg += lg[root] * nods[root].sum;
		codes[root] = code;
	}
	else
	{
		DFS(nods[root].l, code << 1 , level + 1);
		DFS(nods[root].r, (code << 1) + 1, level + 1);
	}
}

void solve()
{
	
	while(r1 - l1 + r2 - l2 != 1)
	{
		int min1 = extractMin();
		int min2 = extractMin();

		HNod & nod = nods[alloc];
		
		nod.sum = nods[min1].sum + nods[min2].sum;
		nod.l = min1;
		nod.r = min2;
		q2[r2++] = alloc;
		alloc++;
	}

	DFS(q2[l2], 0, 0);

}

void afisare()
{
	ofstream fout(outfile.c_str());

	fout << totalLg << "\n";

	for(int i = 0; i < N; i++)
	{
		fout << lg[i] << " " << codes[i] << "\n";
	}

	fout.close();
}

int main()
{
	citire();
	solve();
	afisare();
}