USACO Barn Repair最佳方案
最佳方案不假!
修理牛棚
在一个暴风雨的夜晚,农民约翰的牛棚的屋顶、门被吹飞了。好在许多牛正在度假,所以牛棚没有住满。 剩下的牛一个紧挨着另一个被排成一行来过夜。 有些牛棚里有牛,有些没有。 所有的牛棚有相同的宽度。自门遗失以后,农民约翰很快在牛棚之前竖立起新的木板。 他的新木材供应者将会供应他任何他想要的长度,但是供应者只能提供有限数目的木板。农民约翰想将他购买的木板总长度减到最少。 给出 M(1<= M<=50),可能买到的木板最大的数目;S(1<=S<=200),牛棚的总数;C(1 <= C <=S) 牛棚里牛的数目,和牛所在的牛棚的编号stall_number(1<= stall_number <= S),计算拦住所有有牛的牛棚所需木板的最小总长度。 输出所需木板的最小总长度作为的答案。
| 第 1 行: | M , S 和 C(用空格分开) |
| 第 2 到 C+1行: | 每行包含一个整数,表示牛所占的牛棚的编号。 |
SAMPLE INPUT (file barn1.in)
4 50 18
3
4
6
8
14
15
16
17
21
25
26
27
30
31
40
41
42
43
output:
25
#include"iostream"
#include"algorithm"
#include"stdio.h"
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
//freopen("barn1.in","r",stdin);
//freopen("barn1.out","w",stdout);
int i,m,s,c;
int cows[210];
int ans[210];
cin>>m>>s>>c;
if(m>=c)//能提供的木块比牛多,直接输出牛数
cout<<c<<endl;
else{
for(i=0;i<c;i++)
cin>>cows[i];
sort(cows,cows+c);
ans[0]=cows[1]-cows[0]-1;
for(i=1;i<c-1;i++)
ans[i]=cows[i+1]-cows[i]-1;//求每头牛之间的间隔
sort(ans,ans+c-1,cmp);
int total=cows[c-1]-cows[0]+1;//头和尾两头牛的间隔
for(i=0;i<m-1;i++)//m块木块就去间隔最大的m-1个
total-=ans[i];
cout<<total<<endl;
}
return 0;
}
浙公网安备 33010602011771号