您的位置:首页 > 大数据 > 人工智能

USACO-Section1.3 Barn Repair

2017-06-02 21:50 387 查看

Barn Repair

2017.05.30

题解

贪心法,记录仓间的间隔,用总长度减去最大的M-1个间隔即可

代码

/*
ID: xhzdcyy1
PROB: barn1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#define cin fin
#define cout fout
using namespace std;
ofstream fout ("barn1.out");
ifstream fin ("barn1.in");
bool arr[200]={false};
int inter[200];
bool cmp(int a,int b){
return a-b>0;
}
int main()
{
int m,s,c;
cin>>m>>s>>c;
int min=200,max=0;
for(int i=0;i<c;i++){
int tmp;
cin>>tmp;
if (tmp>max) max=tmp;
if (tmp<min) min=tmp;
arr[tmp]=true;
}
int tmp1=0,cc=0;
//  cout<<min<<" "<<max<<endl;
for(int i=min+1;i<=max;i++){
if(!arr[i]){
++tmp1;
}
else{
if(tmp1){
inter[cc++]=tmp1;
tmp1=0;
}
}
}
sort(inter,inter+cc,cmp);
int res=max-min+1;
for(int i=0;i<m-1;i++){
res-=inter[i];
}
cout<<res<<endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  usaco c++