记录编号 22462 评测结果 AAAAAAAAAA
题目名称 象棋比赛 最终得分 100
用户昵称 Gravatarfanzeyi 是否通过 通过
代码语言 C 运行时间 0.128 s
提交时间 2010-11-19 14:20:22 内存使用 0.23 MiB
显示代码纯文本
/*
 * ===========================================
 *
 * 	Task: Chess
 * 	User: fanzeyi
 * 	Lang: C
 *
 * ===========================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void quicksort(long *arrstart, long s, long t) //arrstart : 數組開始位置指針; s,t:排序的起始位置
{	if (s >= t) return;
	long i = s, j = t, x = arrstart[rand() % (t - s + 1) + s], tmp;
	while (i <= j)
	{ 
		while ((i < t)&&(arrstart[i] < x)) ++i;
		while ((j > s)&&(arrstart[j] > x)) --j;
		if (i <= j)
	 	{	tmp = arrstart[i];
			arrstart[i] = arrstart[j];
			arrstart[j] = tmp;
			++i; --j;}
	}
	quicksort(arrstart, s, j);
	quicksort(arrstart, i, t);
} 

//Orz CCF
//Spring Brother bless me.
int main()
{
	FILE *fin=fopen("chess.in","r");
	FILE *fout=fopen("chess.out","w");
	long n,k;
	fscanf(fin,"%ld %ld\n",&n,&k);
	long *level,*cha;
	level=(long*)malloc(sizeof(long)*n);
	cha=(long*)malloc(sizeof(long)*n);
	int i;
	for(i=0;i<n;i++)
		fscanf(fin,"%ld\n",&level[i]);
	quicksort(level,0,n-1);
	for(i=0;i<n-1;i++)
		cha[i]=abs(level[i+1]-level[i]);
	quicksort(cha,0,n-2);
	long total=0;
	for(i=0;i<k;i++)
		total+=cha[i];
	fprintf(fout,"%ld",total);
	return 0;
}