探测器路径最短总和问题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]
- C 语言代码查错:修正 `get_char` 函数逻辑,避免 `getchar()` 调用错误(DeepSeek | 519点数解答 | 2025-02-07 12:41:36)243
- C++代码:前序创建与遍历二叉树全解析(GPT | 84点数解答 | 2024-11-13 11:44:09)254
- 二叉树前序创建与遍历代码:错误修正、优化及注意要点(百度文心 | 705点数解答 | 2024-11-13 11:44:54)277
- C 语言代码问题修正:规范格式与处理 `fgets` 换行符(字节豆包 | 541点数解答 | 2025-11-30 12:03:00)65
- 6-22:精准删除字符串指定字符,代码逻辑与注意点全解析(字节豆包 | 211点数解答 | 2025-01-21 21:18:10)287
- 巧用C++代码:分离混合字串中的大小写字母!(字节豆包 | 508点数解答 | 2025-12-20 19:18:35)46
- 分离混乱ASCII大小写字母:代码实现与详细解释(字节豆包 | 360点数解答 | 2026-01-08 12:56:47)67
- ASCII 码大小写字母分离代码实现(字节豆包 | 558点数解答 | 2026-03-14 19:25:14)8
- 利用C++实现大小写字母串分离(字节豆包 | 399点数解答 | 2026-04-25 19:21:23)15
- Windows环境下C/C++项目开发:完整目录、文件路径及编译脚本设置示例(字节豆包 | 459点数解答 | 2024-11-02 20:38:01)259
- Python:用前缀树统计字符串数组中以前缀开头的元素数量(字节豆包 | 259点数解答 | 2024-10-26 14:44:39)282
- C++ 代码揭秘:字符 'C' 加 3 竟输出 'F',开发中这些坑要警惕! (字节豆包 | 339点数解答 | 2026-02-01 09:32:56)29