盒子連續,蓋子最少

#include <iostream>
#include <stdio.h>
using namespace std;

int M, S, N, len, temp, id, st, en;
struct se{
    int s;
    int e;    
}s[200010];

int cmp(se a, se b){
    return (a.s-a.e)<(b.s-b.e);    
}

int
main()
{
    int  max, min;
    bool map[200010];
    while(cin>>M>>S>>N ,M||S||N){
        id=0;
        max=0;
        min=200000;
        //memset(map, false, sizeof(bool));
        
        for(int i=1; i<=N; i++){
            cin>>temp;    
            if(max<temp)max=temp;
            if(min>temp)min=temp;
            map[temp]=true;
        }
        /*for(int i=min; i<=max; i++){
            cout<<map[i]<<endl;    
        } */
       // cout<<min<<"min max"<<max<<endl;
        /*while(i<max){
            i++;
            if(map[i]==false){
                st=i;
                while(i<=N){
                    i++;
                    if(map[i]==false)
                        continue;
                    else
                        break;    
                }
                en=i-1;
                s[id].s=st;
                s[id++].e=en;
                cout<<st<<"**"<<en<<endl;
            }  
        }*/
int num = 1, i = 1, j = M;
        while( !map[i] && i <= M ) i++;
        while( !map[j] && j > 0 && j > i ) j--;
        int len = j - i + 1;
        while ( i <= j ) {
            if ( !map[i] ) {
                st = i;
                for ( i++; i <= j; ++i ) if ( !map[i] ) continue; else break;
                en = i;
                s[id].s = st, s[id++].e = en;
            }
            else i++;
        }
        sort(s, s+id, cmp);
        if(id==0) 
            cout<<N<<endl;
        else{
            len=max-min;
            for(int i=0; i<id && i+1<M && len>0; i++){
                len-=(s[i].e-s[i].s);    
            }
            cout<<len<<endl;
        }
    }    
    return 0;
}

转载于:https://my.oschina.net/dianpaopao/blog/122223

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值