Pagini recente » Cod sursa (job #2700721) | Cod sursa (job #2928172) | Cod sursa (job #2335345) | Cod sursa (job #301266) | Cod sursa (job #972623)
Cod sursa(job #972623)
#include <fstream>
#include <queue>
#include <cmath>
#define In "huffman.in"
#define Out "huffman.out"
#define min(a,b) ((a)<(b)?(a):(b))
#define LL long long
#define PLLN pair < LL , node* >
#define PLI pair < LL , int >
#define Nmax 1000010
using namespace std;
struct node
{
node *l, *r;
int ind;
node()
{
ind = 0;
l = r = NULL;
}
};
PLI sol[Nmax];
node *Root = new node;
queue< PLLN >Q[2];
int N;
LL Sum;
inline void Read()
{
ifstream f(In);
f>>N;
int x;
node *A;
for(int i = 1;i <= N ; ++i)
{
f>>x;
A = new node;
A->ind = i;
Q[0].push(make_pair(x,A));
}
f.close();
}
inline PLLN Min()
{
PLLN A;
A.first = 0;
if(Q[0].empty())
{
if(Q[1].empty())
return A;
A = Q[1].front();
Q[1].pop();
return A;
}
if(Q[1].empty())
{
A = Q[0].front();
Q[0].pop();
return A;
}
if(Q[0].front()<Q[1].front())
{
A = Q[0].front();
Q[0].pop();
}
else
{
A = Q[1].front();
Q[1].pop();
}
return A;
}
inline void BuildTree()
{
PLLN X, Y;
node *A,*Last;
while(true)
{
X = Min();
Y = Min();
if(X.first<=0 || Y.first<=0)
{
Root = Last;
return;
}
A = new node;
A->l = X.second;
A->r = Y.second;
Sum += X.first+Y.first;
Last = A;
Q[1].push(make_pair(X.first+Y.first,A));
}
}
inline void BuildSol(node *_node,const LL X, const int length)
{
if(_node->ind)
{
sol[_node->ind].first = X;
sol[_node->ind].second = length;
return;
}
if(_node->l)
BuildSol(_node->l,(X<<1),length+1);
if(_node->r)
BuildSol(_node->r,(X<<1)+1,length+1);
}
inline void Write()
{
ofstream g(Out);
g<<Sum<<"\n";
for(int i = 1;i <= N; ++i)
g<<sol[i].second<<" "<<sol[i].first<<"\n";
g.close();
}
int main()
{
Read();
BuildTree();
BuildSol(Root,0,0);
Write();
return 0;
}