UVa:1554 Binary Search

这个题直接枚举i+1到10000就行。

但是题目哪里说N最大是10000呢。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#define ll long long
#define INF 2139062143
#define inf -2139062144
#define MOD 20071027
#define MAXN 10005
#define LEN 222222<<2
using namespace std;
int u,L;
bool Judge(int val)
{
    int low=0,high=val-1,mid=0;
    for(int i=1; i<=L; ++i)
    {
        mid=(low+high)>>1;
        if(u<mid) high=mid-1;
        else low=mid+1;
        if(mid==u) return i==L;
    }
    return mid==u;
}
bool OK[MAXN];
int main()
{
    while(scanf("%d%d",&u,&L)!=EOF)
    {
        memset(OK,0,sizeof(OK));
        int ans=0;
        for(int i=u+1; i<=10000; ++i)
        {
            if(Judge(i))  OK[i]=true;
            else if(OK[i-1]) ans++;
        }
        if(OK[10000]) ans++;
        printf("%d\n",ans);
        if(ans)
        {
            bool flag=false;
            for(int i=u+1; i<=10001; ++i)
            {
                if(!flag&&OK[i])
                {
                    printf("%d",i);
                    flag=true;
                }
                if(flag&&!OK[i])
                {
                    printf(" %d\n",i-1);
                    flag=false;
                }
            }
        }
    }
    return 0;
}


 

你可能感兴趣的:(UVa:1554 Binary Search)