二分 by zzt

#include <bits/stdc++.h>
using namespace std;

/*
 Problem description:
 There is an array A, the length of array A is N.
 You need to perform Q queries.
 Each query, you get an integer X, and you need to find the smallest integer Y in array A, meet Y > X.
 If there exist such integer, please print the value of the integer.
 Otherwise, please print "There is no number bigger than X".
 
 If you finish the above problem, please try the following problem:
 1. Find the biggest integer Y in array A, Y < X.
 2. Validate if there is a integer Y in array A, meet Y equals to X.
 
 最后写这些:(key 就是上文中每次输入的 X)
 1. 对于不下降序列 a,求最小的 i,使得 a[i] = key
 2. 对于不下降序列 a,求最大的 i,使得 a[i] = key
 3. 对于不下降序列 a,求最小的 i,使得 a[i] > key
 4. 对于不下降序列 a,求最大的 i,使得 a[i] < key
 5. 对于不上升序列 a,求最小的 i,使得 a[i] = key
 6. 对于不上升序列 a,求最大的 i,使得 a[i] = key
 7. 对于不上升序列 a,求最小的 i,使得 a[i] < key
 8. 对于不上升序列 a,求最大的 i,使得 a[i] > key
 
 */

const int maxn = 1e5 + 10;
int n, q;
long long a[maxn];

int main() {
  scanf("%d", &n);
  for(int i = 1; i <= n; i ++) {
    scanf("%lld", &a[i]);
  }
  sort(a + 1, a + 1 + n);
  
  scanf("%d", &q);
  while(q --) {
    long long x;
    scanf("%lld", &x);
    int L = 1, R = n, pos = -1;
    while(L <= R) {
      int mid = (L + R) / 2;
      if(a[mid] <= x) L = mid + 1;
      else R = mid - 1, pos = mid;
    }
    if(pos == -1) printf("There is no number bigger than %lld", x);
    else printf("%lld\n", a[pos]);
  }
  
  return 0;
}

  

转载于:https://www.cnblogs.com/zlrrrr/p/9992023.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值