解析
一道贪心题,看了看luogo的题解,发现我的解法还是不太一样的。
大致思路是:既然要求在限定的板数内覆盖的空栏最少,那么其实问题可以转换为,
对一个连续完整的板(覆盖从头到尾),想办法切割成几块板子,使得切出来的空隙尽量大。
这样解的话,就只需要把连续空缺栏的长度排个序就可以了。
代码
/*
PROG:barn1
ID:imking022
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int cot[103],hod[201];
bool cmp(int a,int b){ return a>b;};
int main(void){
freopen("barn1.out","w",stdout);
freopen("barn1.in","r",stdin);
int m,s,c,k;
cin>>m>>s>>c;
int cur,h,t,pre;
for(int i=0;i<c;i++){
cin>>cur;
hod[cur]++;
}
h = 0 , t = 0, k = 0;
for(int i=1;i<=200;i++){
if(hod[i] == 1){
cur = i;
if(h == 0) { h = i; pre = i;}// mark start point as h
if(i>t) t = i; // mark end point as t
if(cur - pre > 1){
cot[k++] = cur - pre - 1;
}
pre = cur;
}
}
sort(cot,cot+k,cmp);
int sum = t - h + 1;
for(int i=0;i<m-1;i++){// m-1 stands for (m-1) gaps
sum -= cot[i];
}
cout<<sum<<endl;
return 0;
}