Description
在一个夜黑风高,下着暴风雨的夜晚,农民约翰的牛棚的屋顶、门被吹飞了。 好在许多牛正在度假,所以牛棚没有住满。 剩下的牛一个紧挨着另一个被排成一行来过夜。 有些牛棚里有牛,有些没有。 所有的牛棚有相同的宽度。 自门遗失以后,农民约翰必须尽快在牛棚之前竖立起新的木板。 他的新木材供应商将会供应他任何他想要的长度,但是供应商只能提供有限数目的木板。 农民约翰想将他购买的木板总长度减到最少。 给出:可能买到的木板最大的数目M(1<= M<=50);牛棚的总数S(1<= S<=200); 牛棚里牛的总数C(1 <= C <=S);和牛所在的牛棚的编号stall_number(1 <= stall_number <= S),计算拦住所有有牛的牛棚所需木板的最小总长度。 输出所需木板的最小总长度作为答案。
Input
1 行: M , S 和 C(用空格分开) 2 到 C+1行: 每行包含一个整数,表示牛所占的牛棚的编号。
Output
单独的一行包含一个整数表示所需木板的最小总长度。
Sample Input
4 50 18 3 4 6 8 14 15 16 17 21 25 26 27 30 31 40 41 42 43
Sample Output
25
思路:求出每个牛之间间隔的距离,然后最后从总的牛棚减去求出的四个木板卡出的三个最大距离,然后减去开头和末尾距离
得出结果
根据样例举例子可得先求出四个木板之间的空隙和两边的空隙即可
代码:
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
typedef long long LL;
const long long INF = 0x3f3f3f3f;
const long long mod = 1e9+7;
const double PI = acos(-1.0);
const int maxx = 125000;
#define MAX 10010
using namespace std;
int a[120][120],flag[120][120];
int main()
{
int m,s,c;
cin>>m>>s>>c;
for(int i=0; i<c; i++)
{
cin>>a[i];
}
sort(a,a+c);
for(int i=0; i<c; i++)
{
len[i]=a[i+1]-a[i]-1;//木板之间的空隙
}
sort(len,len+c-1);
int ans=0;
for(int i=0; i<m-1; i++)
{
ans+=len[c-i-2];
}
cout<<s-ans-(a[0]-1)-(s-a[c-1])<<endl;
return 0;
}