题解:P4612 [COI 2012] SETNJA
Link
维护一个当前可达矩形区域
对于下一个朋友的矩形
其中
那么答案增加
正确性证明:每一步的最优决策都是将当前可达范围尽量扩大(通过走
时间复杂度:
::::success[AC 代码]
#include <iostream>
void solve() {
int n, x, y, P;
std::cin >> n >> x >> y >> P;
int xmin = x - P, xmax = x + P;
int ymin = y - P, ymax = y + P;
long long ans = 0;
for (int i = 1; i < n; ++i) {
std::cin >> x >> y >> P;
int nxmin = x - P, nxmax = x + P;
int nymin = y - P, nymax = y + P;
int dx = 0, dy = 0;
if (xmax < nxmin) {
dx = nxmin - xmax;
} else if (xmin > nxmax) {
dx = xmin - nxmax;
}
if (ymax < nymin) {
dy = nymin - ymax;
} else if (ymin > nymax) {
dy = ymin - nymax;
}
int d = std::max(dx, dy);
ans += d;
xmin = std::max(xmin - d, nxmin);
xmax = std::min(xmax + d, nxmax);
ymin = std::max(ymin - d, nymin);
ymax = std::min(ymax + d, nymax);
}
std::cout << ans << '\n';
}
int main() {
// freopen("friend.in", "r", stdin);
// freopen("friend.out", "w", stdout);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int _T = 1;
// std::cin >> _T;
while (_T--) {
solve();
}
return 0;
}
::::