Run ID Author Problem Lang Verdict Score Time Memory Code Length Submit Time
346684 admin_cgn 【C6-8】二分查找右侧边界 C++ Time Limit Exceeded 40 1000 MS 304 KB 753 2025-09-11 14:07:05

Tests(4/10):


#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N], n, q; // 返回 x 在 a[] 中最后一次出现的下标,不存在返回 -1 int lastPos(int x) { int l = 1, r = n; while (l < r) { int mid = (l + r + 1) >> 1; // 向上取整 if (a[mid] <= x) l = mid; // 答案在右半 [mid, r] else r = mid - 1; // 答案在左半 [l, mid-1] } // 循环结束后 l==r,再验证是否真的是 x return a[l] == x ? l : -1; } int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; cin >> q; while (q--) { int x; cin >> x; cout << lastPos(x) << (q ? ' ' : '\n'); } return 0; }


Judgement Protocol: