题意
- 有 个物品,分别需要从 运输到 处, 处有一个传送门入口,选择一个传送门出口 ,使得总运输距离 最小,输出最小值。
分析
- 很明显,传送门对于总距离 的贡献是可以拆分成对于每一个物品的贡献之和的,因为有 。
- 然后我们分析一下 的选址对于每一个物品的 的贡献。
- 通过画图和分类讨论可以发现,一共有两种情况,而且每个 对应的 是分段且线性的,根据线性函数的一些性质,我们可以利用对斜率的差分来求解 。
画图
- 这时候肯定不会走传送门的,因为运去 那里都比直接运要远。




- 如图所示,这种情况下的三个分段点是 , 和 。
- 而且,这三种情况的初始值均为 ,所以初始值 要设成 。
实现
- 对于每一个物品,根据上图分类后利用 map 来存储差分的数组。对第一个分段位置 ,对第二个 ,最后一个 。处理完之后按 的值从小到大遍历 map,对于每一个 的分段点的值求最小值即可。
代码
| #include <bits/stdc++.h> |
| #define inf 0x7fffffff |
| #define int long long |
| using namespace std; |
| int n, x, y = -inf, s, ans; |
| map<int, int> mp; |
| |
| inline void read(int &x) { |
| int w = 1; |
| char ch = x = 0; |
| while (ch < '0' || ch > '9') { |
| if (ch == '-') w *= -1; |
| ch = getchar(); |
| } |
| while(ch >= '0' && ch <= '9') { |
| x = (x << 1) + (x << 3) + ch - 48; |
| ch = getchar(); |
| } |
| x *= w; |
| return ; |
| } |
| |
| signed main() { |
| read(n); |
| int a, b; |
| for (int i = 1; i <= n; i++) { |
| read(a), read(b); |
| x += abs(a - b); |
| if (abs(a) > abs(a-b)) continue; |
| mp[b] += 2; |
| if ((a < b && a < 0) || (a >= b && a >= 0)) { |
| mp[0]--; |
| mp[b << 1]--; |
| } else if ((a < b && a >= 0) || (a >= b && a < 0)) { |
| mp[(b - a) << 1]--; |
| mp[a << 1]--; |
| } |
| } |
| ans = x; |
| int now, tmp; |
| for (auto it : mp) { |
| now = it.first, tmp = it.second; |
| x += s * (now - y); |
| y = now; |
| s += tmp; |
| ans = min(ans, x); |
| } |
| printf("%lld", ans); |
| return 0; |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战