1 条题解
-
0
#include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; } int m; cin >> m; while (m--) { int x; cin >> x; auto it = lower_bound(arr.begin(), arr.end(), x); int pos = it - arr.begin(); int result; if (pos == 0) { result = arr[0]; } else if (pos == n) { result = arr[n - 1]; } else { int a = arr[pos - 1]; int b = arr[pos]; if (abs(a - x) < abs(b - x)) { result = a; } else if (abs(a - x) > abs(b - x)) { result = b; } else { result = a; } } cout << result << endl; } return 0; }
- 1
信息
- ID
- 376
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 9
- 已通过
- 3
- 上传者