酷代码 AI
菜单
服务商
DeepSeek字节豆包
更多选项

探测器路径最短总和问题Ch+求解

```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> pipes(n); for (int i = 0; i < n; i++) cin >> pipes[i]; vector<int> probes(m); for (int i = 0; i < m; i++) cin >> probes[i]; // 管道口排序 sort(pipes.begin(), pipes.end()); long long total = 0; // 总和可能较大,用long long for (int i = 0; i < m; i++) { int pos = probes[i]; // 二分查找第一个大于等于pos的管道口位置 auto it = lower_bound(pipes.begin(), pipes.end(), pos); // 取左右距离的最小值 if (it == pipes.begin()) { total += abs(*it - pos); // 只有右侧 } else if (it == pipes.end()) { total += abs(*(it - 1) - pos); // 只有左侧 } else { int right = *it; int left = *(it - 1); total += min(abs(left - pos), abs(right - pos)); } } cout << total << endl; return 0; } ``` ######[AI问答 | 309点数解答 | 2026-06-06 19:37:36]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]