AcWing 1349. 修理牛棚
原题链接
中等
作者:
偷月亮的喵
,
2025-01-03 15:08:05
,
所有人可见
,
阅读 3
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 210;
int m, s, c;
int a[N], b[N];
int main()
{
cin >> m >> s >> c;
for (int i = 1; i <= c; i++) cin >> a[i];//牛所占牛棚的编号
sort(a + 1, a + c + 1);
for (int i = 2; i <= c; i++) b[i - 1] = a[i] - a[i - 1] - 1;//相邻牛之间的牛棚数
sort(b + 1, b + c);
int ans = c;//先每一牛一块要木板
if(m < c){//如果木板小于牛数
for (int i = 1; i <= c - m; i++){
if(b[i] > 0)
ans += b[i];//加空档
}
}
cout << ans << endl;
return 0;
}