1.3.2 Barn Repair【贪心】

来源:互联网 发布:武汉 人工智能 编辑:程序博客网 时间:2024/06/05 17:28

链接

/*ID: email_f1LANG: C++PROG: barn1*/#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <set>#include <map>#include <stack>#include <queue>#include <vector>#include <iostream>#include <algorithm>using namespace std;#define ll long long#define eps 10^(-6)#define Q_CIN ios::sync_with_stdio(false);#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define CLR( a , x ) memset ( a , x , sizeof (a) );#define RE freopen("barn1.in","r",stdin);#define WE freopen("barn1.out","w",stdout);#define MOD 10009#define bug(x) cout<<#x<<":"<<(x)<<endl;//用最短的长度将区间覆盖,则给的木板数当然都用上最省了,也就是说用M根板尽量短地覆盖//首先假设用板将全部有牛的覆盖掉,然后每次挑里面最大的空隙,在那里切开(覆盖到没牛的棚当然要尽量少),直到木板没了。//如果木板数大于有牛的数,则长度为牛数//空隙:6-5=1但没有空隙,7-5才有int a[250],len[250];bool cmp(int a,int b){    return a>b;}int main(){    RE WE    int m,s,c;    while(cin>>m>>s>>c)    {        REP(i,c)            cin>>a[i];        int ans;        if(m>=c)        //如果木板足够,则有多少个带牛的牛棚就多少长度            ans=c;        else        {            sort(a,a+c);            //编号不一定升序            FOR(i,1,c)                len[i]=a[i]-a[i-1]-1;            len[0]=0;            ans=a[c-1]-a[0]+1;      //最后个有牛-第一个有牛            sort(len,len+c,cmp);    //长度降序排序            int tmp=0,idx=0;        //idx=0            m--;                    //先给全部搭成一块,剩余块数-1            while(m--)              //每切一刀多一块,则剩余的块数减少1            {                ans-=len[idx];                idx++;            }        }        cout<<ans<<endl;    }    return 0;}


0 0