您的位置:首页 > 大数据 > 人工智能

Barn Repair

2016-05-09 19:39 399 查看
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++ 贪心