hdu4151(二分)

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4151

 

题意:找出比n小的没有重复数字的总个数,例如12以内11不符合,1~10都符合。

 

分析:直接利用lower_bound函数找出比n刚好大的位置再减一就是答案。这里a数组从0开始,所以不用减一。

 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 1<<30
#define N 10000000
using namespace std;
int a[1000000],tot;
int vis[10];
void init()
{
    tot=0;
    for(int i=1;i<=N;i++)
    {
        int temp=i;
        memset(vis,0,sizeof(vis));
        while(temp)
        {
            int t=temp%10;
            if(!vis[t])vis[t]=1;
            else break;
            temp/=10;
        }
        if(!temp)a[tot++]=i;
    }
}
int main()
{
   int n;
   init();
   while(scanf("%d",&n)>0)
   {
      // printf("%d\n",lower_bound(a,a+tot,n)-a);
       int left=0,right=tot,ans=0;
       while(left<right)
       {
          int mid=left+(right-left)/2;
           if(a[mid]>=n)
           {
               right=mid;
           }
           else left=mid+1;
       }
       printf("%d\n",right);
   }
}
View Code

 

转载于:https://www.cnblogs.com/lienus/p/4147735.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值