二分
作者:
Asukaズ
,
2024-07-20 10:58:57
,
所有人可见
,
阅读 8
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, m;
int q[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++) scanf("%d", &q[i]);
// 二分主要看else位置的变化
for (int i = 0; i < m; i ++)
{
int x;
scanf("%d", &x);
int l = 0, r = n - 1;
while (l < r)
{
int mid = (l + r) >> 1;
if (q[mid] > x) r = mid;
else l = mid + 1;
}
int l = 0, r = n - 1;
while (l < r) // qmid 小于x 后将范围改为[mid, r];大于x将范围改为[l, mid - 1];
{
int mid = l + r + 1 >> 1;
if (q[mid] < x) l = mid;
r = mid - 1;
}
}
return 0;
}