| 比赛 | 20101119 | 评测结果 | WWWWWWWWWW | 
    | 题目名称 | 象棋比赛 | 最终得分 | 0 | 
    | 用户昵称 | fanzeyi | 运行时间 | 0.000 s | 
    | 代码语言 | C | 内存使用 | 0.00 MiB | 
    | 提交时间 | 2010-11-19 09:32:12 | 
显示代码纯文本
/*
 * =========================================
 *
 * 	Task: Chess
 * 	User: fanzeyi
 * 	Lang: C
 *
 * =========================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void quicksort(int* arrstart, int s, int t) //arrstart : 數組開始位置指針; s,t:排序的起始位置
{	if (s >= t) return;
	int 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
int main()
{
	FILE *fin=fopen("chess.in","r");
	FILE *fout=fopen("chess.out","w");
	int n,k;
	fscanf(fin,"%d %d\n",&n,&k);
	int i;
	long *level,*cha;
	level=(long*)malloc(sizeof(long)*n);
	cha=(long*)malloc(sizeof(long)*n);
	for(i=0;i<n;i++)
		fscanf(fin,"%d",&level[i]);
	quicksort(level,0,n);
	for(i=0;i<n-1;i++)
		cha[i]=level[i+1]-level[i];
	quicksort(cha,0,n-1);
	int total=0;
	for(i=0;i<k;i++)
		total+=cha[i];
	fprintf(fout,"%d",total);
	return 0;
}