比赛 20101118 评测结果 WWWWWAAWWW
题目名称 分班 最终得分 20
用户昵称 fanzeyi 运行时间 0.000 s
代码语言 C 内存使用 0.00 MiB
提交时间 2010-11-18 11:05:38
显示代码纯文本
/*
 * ===============================================
 *
 * 	Task: Groupa
 * 	User: fanzeyi
 * 	Lang: C
 *
 * ===============================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long L,R;
int count=0;
short *class;
long *number;

void avag(int i,int j)
{
	if((number[i]+number[j])/2<=R&&(number[i]+number[j])/2>=L)
	{
		count+=number[i]-(number[i]+number[j])/2;
		number[i]=(number[i]+number[j])/2;
		number[j]=number[i];
		class[i]=0;
		class[j]=0;
	}
}

int main()
{
	FILE *fin=fopen("groupa.in","r");
	FILE *fout=fopen("groupa.out","w");
	int n;
	long total=0;
	fscanf(fin,"%d\n",&n);
	number=(long*)malloc(sizeof(long)*n);
	class=(short*)malloc(sizeof(short)*n);
	int i,j;
	for(i=0;i<n;i++)
	{
		fscanf(fin,"%ld ",&number[i]);
		total+=number[i];
	}
	fscanf(fin,"%ld %ld",&L,&R);
	if(L*n>total||total>R*n)
	{
		printf("-1");
		return 0;
	}
	for(i=0;i<n;i++)
		if(number[i]<L)
			class[i]=-1;
		else if(number[i]>R)
			class[i]=1;
		else 
			class[i]=0;
	for(i=0;i<n;i++)
	{
		if(class[i]==1)
		{
			for(j=0;j<n;j++)
				if(class[j]==-1)
					avag(i,j);
			if(class[i]==1)
			{
				for(j=0;j<n;j++)
					if(class[j]==0)
						avag(i,j);
			}
		}
		if(class[i]==-1)
		{
			for(j=0;j<n;j++)
				if(class[j]==0)
					avag(i,j);
		}
	}
	fprintf(fout,"%d",count);
	return 0;
}