ZZULIOJ 2856: 小A的游戏制作系列一(lower_bound)

2856: 小A的游戏制作系列一

二分查找lower_bound的使用

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1000010;
int n,m;
int a[N];
  
signed main()
{
    
    
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    sort(a,a+n);
    reverse(a,a+n);
    while(m--)
    {
    
    
        int x;scanf("%d",&x); 
        int pos=*lower_bound(a,a+n,x,greater<int>());//二分查找//在数据库里小于等于x的最大数值 
        if(pos==0) printf("%d\n",a[0]);//找不到比它小的 
        else printf("%d\n",pos);
    }
    return 0;
}

不懂lower_bound的可以看一下这个

lower_bound函数和upper_bound函数

猜你喜欢

转载自blog.csdn.net/qq_52792570/article/details/121426786