Cod sursa(job #1715255)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 10 iunie 2016 10:45:56
Problema Coduri Huffman Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.16 kb
using namespace std;
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define Nmax 1000000

struct _node { int ord; ll freq; _node *l, *r; };
class _queue
{
private:
	_node *Q1[Nmax], *Q2[Nmax];
	int l1, r1, l2, r2;

public:
	_queue() : l1(0), r1(-1), l2(0), r2(-1) {}

	void init(const vector< int > &v)
	{
		for (int i = 0; i < static_cast<int>(v.size()); ++i)
		{
			_node *temp = new _node;
			temp->l = temp->r = nullptr;
			temp->freq = v[i];
			temp->ord = i;

			Q1[++r1] = temp;
		}
	}

	_node* getMin()
	{
		_node *ret;

		if (l1 > r1) ret = Q2[l2], ++l2;
		else if (l2 > r2) ret = Q1[l1], ++l1;
		else if (Q1[l1]->freq < Q2[l2]->freq)
		{
			ret = Q1[l1];
			++l1;
		}
		else
		{
			ret = Q2[l2];
			++l2;
		}

		return ret;
	}

	void push(_node *node)
	{
		Q2[++r2] = node;
	}

	int size()
	{
		return static_cast<int>(r1 - l1 + 1 + r2 - l2 + 1);
	}
} Q;

int n;
vector< int > freq, lg;
vector< ll > codes;

void read();
void getCodes(_node*, ll, int);
void write();

int main()
{
	_node *root;

	read();

	Q.init(freq);
	while (Q.size() > 1)
	{
		root = new _node;
		root->l = Q.getMin();
		root->r = Q.getMin();
		root->freq = root->l->freq + root->r->freq;
		root->ord = -1;

		Q.push(root);
	}

	codes.resize(n);
	lg.resize(n);

	getCodes(root, 0, 0);

	write();

    return 0;
}


void read()
{
	ifstream fin("huffman.in");

	fin >> n;
	freq.resize(n);

	for (int i = 0; i < n; ++i) fin >> freq[i];

	fin.close();
}

void getCodes(_node* node, ll code, int l)
{
	if (!node->l) codes[node->ord] = code, lg[node->ord] = l;
	else
	{
		getCodes(node->l, code << 1, l + 1);
		getCodes(node->r, (code << 1) + 1, l + 1);
	}
}

void write()
{
	int i;
	ll lgCode;
	ofstream fout("huffman.out");

	for (lgCode = 0, i = 0; i < n; ++i)
		lgCode += 1LL * freq[i] * lg[i];

	fout << lgCode << '\n';

	for (i = 0; i < n; ++i)
		fout << lg[i] << ' ' << codes[i] << '\n';

	fout.close();
}