【小贪心】USACO Barn Repair

KIDx的解题报告

 

进入USACO要注册才能看题: http://train.usaco.org/usacogate

题目:【翻译版、是别处的网站】

http://www.wzoi.org/usaco/11%5C304.asp

/*
ID: 1006100071
LANG: C++
TASK: barn1
*/
#include <iostream>
#include <algorithm>
using namespace std;
#define M 205

int a[M];
struct point{
	int d, i;
}x[M];

bool cmp (point a, point b)
{
	return a.d > b.d;
}

bool cmp2 (point a, point b)
{
	return a.i < b.i;
}

int main()
{
	/*freopen ("barn1.in", "r", stdin);
	freopen ("barn1.out", "w", stdout);*/
	int k, n, m, ans = 0, pre, i;
	scanf ("%d%d%d", &k, &m, &n);
	k = min (k, n);		//注意……
	k--;				//分割点个数
	for (i = 0; i < n; i++)
		scanf ("%d", a+i);
	sort (a, a+n);
	for (i = 0; i < n - 1; i++)
		x[i].d = a[i+1] - a[i], x[i].i = i;
	sort (x, x+n-1, cmp);
	sort (x, x+k, cmp2);
	pre = a[0];					//pre是第i个组的起点
	for (i = 0; i < k; i++)
	{
		ans += a[x[i].i] - pre + 1;		//x[i].i第i个组的终点下标
		pre = a[x[i].i+1];		//把下一个元素作为下一组的起点
	}
	ans += a[n-1] - pre + 1;	//最后一组
	printf ("%d\n", ans);
	return 0;
}

 

你可能感兴趣的:(USACO)