求救 绞尽脑汁了还是超时

我会发着呆 发表于 8个月前 · 关联问题 二分查找(基本实现)

include

using namespace std; const int N = 100010; void check(long long arr[], long long a, long long n) {

long long l = 0, r = n - 1; while (l < r) { long long mid = (l + r) >> 1; if (a <= arr[mid])r = mid; else l = mid + 1; } if (arr[l] == a)cout << "YES" << endl; else cout << "NO" << endl;

} int main() {

ios::sync_with_stdio(false); long long n, m; cin >> n >> m; long long arr[N]; for (int i = 0; i < n; i++)cin >> arr[i]; for (int i = 0; i < m; i++) { long long temp = 0; cin >> temp; check(arr, temp, n); } return 0;

}

黄一肯 发表于 8个月前

r = mid - 1 考虑到最后 l = r的情况,这时候会一直卡在while上

Z2470522057 发表于 7个月前