Barn Repair

http://train.usaco.org/usacoprob2?a=IbVlSXKH9iW&S=barn1

题目大意:略

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int a[200], b[200];
int main()
{
	ifstream fin("barn1.in");
	ofstream fout("barn1.out");
	int m, s, c, ans;
	while(fin >> m >> s >> c)
	{
		for(int i = 0; i < c; i++) fin >> a[i];
		sort(a, a + c);
		for(int i = 0; i < c - 1; i++) b[i] = a[i + 1] - a[i] - 1;
		sort(b, b + c - 1);
		ans = 0;
		for(int i = c - 2, j = 0; i >= 0 && j < m - 1; i--, j++)
			ans += b[i];
		ans = a[c - 1] - a[0] + 1 - ans;
		fout << ans << endl;
	} 
	fout.close();
	return 0;
}


你可能感兴趣的:(C++,贪心)