题目描述
在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶、门被吹飞了。 好在许多牛正在度假,所以牛棚没有住满。 牛棚一个紧挨着另一个被排成一行,牛就住在里面过夜。 有些牛棚里有牛,有些没有。 所有的牛棚有相同的宽度。 自门遗失以后,farmer John必须尽快在牛棚之前竖立起新的木板。 他的新木材供应商将会供应他任何他想要的长度,但是吝啬的供应商只能提供有限数目的木板。 farmer John想将他购买的木板总长度减到最少。
给出:可能买到的木板最大的数目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行: 每行包含一个整数,表示牛所占的牛棚的编号。
输出格式: 单独的一行包含一个整数表示所需木板的最小总长度。
输入输出样例
输入样例#1: 4 50 18 3 4 6 8 14 15 16 17 21 25 26 27 30 31 40 41 42 43 输出样例#1: 25
说明
题目翻译来自NOCOW。
USACO Training Section 1.3
代码
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=
200+
10;
int a[MAXN],b[MAXN];
void read(
int &x)
{
x=
0;
char c=getchar();
while(c<
'0'||c>
'9')c=getchar();
while(c>=
'0'&&c<=
'9')
{
x=x*
10+c-
'0';
c=getchar();
}
}
int comp(
const int&x,
const int&y)
{
return x>y;
}
int main()
{
int m,s,c;
read(m);read(s);read(c);
if(m>=c)
{
printf(
"%d",c);
return 0;
}
m--;
for(
register int i=
1;i<=c;++i)read(a[i]);
sort(a+
1,a+c+
1);
for(
register int i=
1;i<c;++i)b[i]=a[i+
1]-a[i];
int ans=a[c]-a[
1]+
1;
sort(b+
1,b+c,comp);
int j=
1;
while(m--)
{
ans-=b[j]-
1;
j++;
}
printf(
"%d",ans);
return 0;
}