Cod sursa(job #712455)

Utilizator iarbaCrestez Paul iarba Data 13 martie 2012 14:40:52
Problema Tribute Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <stdio.h>
unsigned long s1[50004],s2[50004],t,nord,sud,est,vest,xx,yy,dx,dy,p,m,x,y,s10,s20,ks,kn,ke,kv;
FILE *f,*g;
long i,n;
int main()
{
	f=fopen("tribute.in","r");
	g=fopen("tribute.out","w");
	fscanf(f,"%ld%ld%ld",&n,&dx,&dy);
	s10=n;s20=n;
	for(i=1;i<=n;i++){
		fscanf(f,"%ld%ld",&xx,&yy);
		s10+=xx;s1[xx]++;
		s20+=yy;s2[yy]++;
	                 }
	i=-1;nord=n;sud=0;
	while(nord>sud){i++;s10=s10-nord+sud;nord-=s1[i];sud+=s1[i];}
	x=i;sud-=s1[i];vest=n;est=0;i=-1;
	while(vest>est){i++;s20=s20-vest+est;vest-=s2[i];est+=s2[i];}
	y=i;est-=s2[i];t=s10+s20;ks=1;kn=1;ke=1;kv=1;
	while((dx)&&((nord)||(sud))){
		if((nord)&&(nord>=sud)){t-=nord;dx--;nord-=s1[x+kn];kn++;}
		if((sud)&&(nord<sud)){t-=sud;dx--;sud-=s1[x-ks];ks++;}
	                            }
	while((dy)&&((est)||(vest))){
		if((vest)&&(vest>=est)){t-=vest;dy--;vest-=s1[x+kv];kv++;}
		if((est)&&(vest<est)){t-=est;dy--;est-=s1[y-ke];ke++;}
	                            }
	fprintf(g,"%ld",t);
	fclose(f);fclose(g);
return 0;
}