这道题还是比较简单的,逆向考虑,既然要求连续的长度,不如去求间断的长度。只是有些细节需要注意以下,如M>C,M=1,等情况。代码如下。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main (){
freopen ("barn1.in","r",stdin);
freopen ("barn1.out","w",stdout);
int M=0,S=0,C=0,less=0;
int barn[205];
int sort[205];
int in[205];
memset(sort,0,sizeof(sort));
memset(in,0,sizeof(in));
int i=0,cnt=0,t=0,k=0,count=0;
cin>>M>>S>>C;
for (i=0;i<S;i++){
cin>>k;
in[k]++;
}
if (M>=C){
cout<<C<<endl;
return 0;
}
for (i=0;i<=S;i++){
if (in[i]!=0){
barn[count++]=i;
}
}
for (i=1;i<count;i++){
if ((barn[i]-barn[i-1]-1)==0){
continue;
}
sort[barn[i]-barn[i-1]-1]++;
if (barn[i]-barn[i-1]-1>t){
t=barn[i]-barn[i-1]-1;
}
}
less=barn[C-1]-barn[0]+1;
if (M==1){
cout<<less<<endl;
return 0;
}
while (cnt!=(M-1)){
if (sort[t]!=0){
less-=t;
cnt++;
sort[t]--;
}
if (sort[t]==0){
t--;
}
}
cout<<less;
return 0;
}