比赛 20101119 评测结果 AAAAAAAAAA
题目名称 象棋比赛 最终得分 100
用户昵称 wangwangdog 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2010-11-19 10:44:12
显示代码纯文本
#include<stdio.h>
long n,k,i,level[100001],cha[100001];
long abs(long aaa)
{
	if(aaa<0)return -aaa;
	else return aaa;
}
void qsort (long a,long b)   
{   
    long t1=a,t2=b,t3=level[(a+b)/2];   
    do  
    {   
        while(level[t1]<t3)t1++;   
        while(level[t2]>t3)t2--;   
        if(t1<=t2)   
        {   
            long o=level[t1];   
            level[t1]=level[t2];   
            level[t2]=o;   
            t1++;   
            t2--;   
        }   
    }   
    while(t1<t2);   
    if(t1<b)qsort(t1,b);   
    if(t2>a)qsort(a,t2);   
}   

int main()
{
	FILE *fin,*fout;
	fin=fopen("chess.in","rb");
	fout=fopen("chess.out","wb");
	fscanf(fin,"%ld%ld\n",&n,&k);
	for(i=1;i<=n;i++)
	{
		fscanf(fin,"%ld\n",&level[i]);
	}
	qsort(1,n);
	for(i=1;i<=n-1;i++)
		cha[i]=abs(level[i+1]-level[i]);
	for(i=1;i<=n-1;i++)
		level[i]=cha[i];
	qsort(1,n-1);
	long all=0;
	for(i=1;i<=k;i++)
		all=all+level[i];
	fprintf(fout,"%ld",all);
	fclose(fin);
	fclose(fout);
	return 0;
}