1 条题解
-
0
循环控制
#include <bits/stdc++.h> using namespace std; int n, q, x; int a[1123456]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); //此时 a[n+1] 就是 0 while (q--) { cin >> x; int l = 1; int r = n; int ans = n + 1; while (l <= r) { int mid = (l + r) / 2; if (a[mid] > x) { ans = mid; r = mid - 1; } else l = mid + 1; } cout << a[ans] << "\n"; } return 0; }
STL:upper_bound
#include <bits/stdc++.h> using namespace std; int n, q, x; int a[1123456]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); while (q--) { cin >> x; int pos = upper_bound(a + 1, a + n + 1, x) - a; cout << a[pos] << "\n"; } return 0; }
- 1
信息
- ID
- 1175
- 时间
- 2000ms
- 内存
- 256MiB
- 难度
- 2
- 标签
- 递交数
- 67
- 已通过
- 41
- 上传者