题解:P15331 [GCPC 2025] Demand for Cycling
题解:P15331 [GCPC 2025] Demand for Cycling
前言
思路讲解
题目一堆话,其实一句话就能总结:
在一个二维平面图上画一个周长最短图形,使图形覆盖给定的 \(n\) 个点。
很容易就能想到,这个图形一定是一个矩形。
因为你可以根据这几个点画一个周长最短的图形,把这个图形通过平移成一个矩形,如果不行,那么就代表你画的这个图形周长不是最短的。
我们这个矩形只有四条边,要包含这 \(n\) 个点,说明:
- 上面的边一定在所有节点上面
- 下面的边一定在所有节点下面
- 左边的边一定在所有节点左边
- 右边的边一定在所有节点右边
所以这个矩形的四个点坐标一定为:
- \((max(x),max(y))\)
- \((min(x),min(y))\)
- \((max(x),min(y))\)
- \((min(x),max(y))\)
按顺序输出即可。
AC Code
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int mxx = 0, mxy = 0, mnx = 1e9, mny = 1e9;
for (int i = 1; i <= n; i++)
{
int x, y;
cin >> x >> y;
mxx = max(mxx, x);
mxy = max(mxy, y);
mnx = min(mnx, x);
mny = min(mny, y);
}
cout << "4\n";
cout << mnx << ' ' << mxy << '\n';
cout << mnx << ' ' << mny << '\n';
cout << mxx << ' ' << mny << '\n';
cout << mxx << ' ' << mxy << '\n';
return 0;
}

浙公网安备 33010602011771号