zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",right);
       }
    }
    View Code
  • 相关阅读:
    用户数据报协议---UDP
    斐波那契数列
    从尾到头打印链表
    Mybatis三种查询方式
    Mybatis配置
    字典的用法
    遍历列表、切片、定义元组
    与列表相关知识
    python一些方法总结
    计算机的容量
  • 原文地址:https://www.cnblogs.com/lienus/p/4147735.html
Copyright © 2011-2022 走看看