zoukankan      html  css  js  c++  java
  • CodeForces 706B Interesting drink

    排序,二分。

    将$x$数组从小到大排序,每次询问的时候只要二分一下位置就可以了。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    const double pi=acos(-1.0),eps=1e-8;
    void File()
    {
        freopen("D:\in.txt","r",stdin);
        freopen("D:\out.txt","w",stdout);
    }
    
    const int maxn=100010;
    int n,q,x[maxn];
    
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&x[i]);
        sort(x+1,x+1+n);
        scanf("%d",&q);
        for(int i=1;i<=q;i++)
        {
            int p; scanf("%d",&p);
            int L=1,R=n,pos=-1;
            while(L<=R)
            {
                int mid=(L+R)/2;
                if(x[mid]<=p) pos=mid,L=mid+1;
                else R=mid-1;
            }
            if(pos==-1) printf("%d
    ",0);
            else printf("%d
    ",pos);
        }
        return 0;
    }
  • 相关阅读:
    python之名称空间
    python之对象(实例)
    python之类
    python之类和对象
    python之面向对象的程序设计
    python之函数联系
    Python之函数第三篇
    python之列表和生成器表达式篇
    网络基本概念
    Git
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5810590.html
Copyright © 2011-2022 走看看