盒子連續,蓋子最少

#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;
}

你可能感兴趣的:(盒子連續,蓋子最少)