题解:P12351 「HCOI-R2」哀之距
思路
定义:
数据范围:
因为数据范围太大,我们应当使用 long long。为了确保了无论输入的矩形坐标如何,这些变量都能被正确地更新,我们应将
计算最小覆盖矩形的尺寸
更新
AC Code
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
#define maxn LONG_LONG_MAX
#define minn LONG_LONG_MIN
#define ll long long
#define upto(i,a,b) for(long long i=1;i<=n;i++)
ll a = maxn, b = minn, c = maxn, d = minn;
ll n;
int main() {
ios::sync_with_stdio(false);//关流
cin >> n;
upto(i, 1, n) {
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
a = min(a, x2);
b = max(b, x1);
c = min(c, y2);
d = max(d, y1);
}
ll xx = b - a, yy = d - c;//宽高
ll ans1 = max(xx, (ll)0); //由于可能存在负数情况,使用max(xx, 0ll)和`max(yy, 0ll)确保宽度和高度非负
ll ans2 = max(yy, (ll)0); //0ll即(long long)0
cout << max(ans1, ans2); //宽度和高度中的最大值
return 0;
}
鸣谢
对于求切比雪夫距离最大值,本篇题解在一定程度上参考了 __CJY__ 求极差的思路,感谢TA。