2 条题解
-
0
下面的题解就是一个萝莉,他已经和这个时代渐行渐远了,其实这道题就是一个萝莉,无脑二分即可,会写lower_bound的也可以用这个,不过我是高手,此等题目萝莉一个,无需借用此等法宝,下面直接给出自己手动编写的ac代码
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; int num[100005]; int main() { freopen("closest.in", " r", stdin); freopen("closest.out", "w ", stdout); int n, m, x; cin >> n; for (int i = 1; i <= n; i++) { cin >> num[i]; } cin >> m; sort(num + 1, num + 1 + n); while (m--) { cin >> x; if(x < num[1]){ cout << num[1] << end1; } else if(x > num[n]){ cout << num[n] << endl; } else { int l = 1, r = n; while(l + 1 < r){ int mid = (l + r) / 2; if(num[mid] >= x){ r = mid; } else { l = mid; } } if(abs(num[l] - x) <= abs(num[r] - x)){ cout << num[l] << endl; } else { cout << num[r] << endl; } } } return 0; }直接复制粘贴的也是萝莉。
- 1
信息
- ID
- 402
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- (无)
- 递交数
- 420
- 已通过
- 35
- 上传者