15 Mar 2013

Chef Teams_solved


/*
CODECHEF
PROBLEM CODE : CTEAMS
*/ 
#include <stdio.h>
#include <math.h>
#define tausch(a,b) b=a+b, a=b-a, b=b-a
int X[100100][5];
void f3(int i)
{
int l=2*i, r=2*i+1, z=i;
z=(l<=X[2][4]&&X[X[l][2]][0]>X[X[i][2]][0])?l:z;
z=(r<=X[2][4]&&X[X[r][2]][0]>X[X[z][2]][0])?r:z;
if(z!=i)
{
tausch(X[i][2],X[z][2]);
f3(z);
}
}
void f4(int i)
{
int l=2*i, r=2*i+1, Y=i;
Y=(l<=X[3][4]&&X[X[l][3]][0]<X[X[i][3]][0])?l:Y;
Y=(r<=X[3][4]&&X[X[r][3]][0]<X[X[Y][3]][0])?r:Y;
if(Y!=i)
{
tausch(X[i][3],X[Y][3]);
f4(Y);
}
}
void f5(int i)
{
if(X[2][4]<=X[3][4])
{
X[++X[2][4]][2]=i;
X[0][4]+=X[X[X[2][4]][2]][1];
for(X[4][4]=X[2][4]/2,X[5][4]=X[2][4];
 X[5][4]>1&&X[X[X[4][4]][2]][0]<X[X[X[5][4]][2]][0];
  tausch(X[X[4][4]][2],X[X[5]4]][2]),X[5][4]=X[4][4],X[4][4]/=2);
}
else
{
X[++X[3][4]][3]=i;
X[1][4]+=X[X[X[3][4]][3]][1];
for(X[4][4]=X[3][4]/2,X[5][4]=X[3][4]
X[5][4]>1&&X[X[X[4][4]][3]][0]>X[X[X[5][4]][3]][0];
 tausch(X[X[4][4]][3],X[X[5][4]][3]),X[5][4]=X[4][4],X[4][4]/=2);
}
if((X[X[1][2]][0]>X[X[1][3]][0])&&(X[2][4]>0)&&(X[3][4]>0))
{
X[0][4]-=X[X[1][2]][1];
X[1][4]-=X[X[1][3]][1];
tausch(X[1][2],X[1][3]);
X[0][4]+=X[X[1][2]][1];
X[1][4]+=X[X[1][3]][1];
f3(1);
f4(1);
}
printf("%d\n",abs(X[1][4]-X[0][4]));
}
main()
{
int fall, t;
for(X[2][4]=X[3][4]=X[0][4]=X[1][4]=t=!scanf("%d",&fall);
 t++<fall; 
scanf("%d %d",&X[t][0],&X[t][1]),f5(t));
return 0;

}


Categories: , , ,

0 comments:

Post a Comment

Copyright © UPgradeCODING | Powered by Blogger