CodeForces - 1013C Photo of The Sky (思维)

题目:传送门
思路: 很明显 ,矩形的面积 S = (max(x)-min(x)) * (max(y)-min(y)). 因为点的个数是确定为n的,则一定有 n个数为 x坐标 ,n 个数位 y 坐标 ,我们现在需要讨论如何分组。
先将数组从小到大排序,如果 我们已经选定一个 a[i] ,要保证 其为 min(y) ,则该情况下y坐标的max应该要尽可能的小,则我们应该选择从 i开始的长度 为 n 的区间为 集合,这样是保证max(y)-min(y)最小的,我们每次都这样选择区间 , 讨论所有情况即可.

#include <iostream>
#include <algorithm>
using namespace std;

long long a[200010];

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < 2*n; i++) cin >> a[i];
	sort(a, a + 2*n);
	long long ans = (a[2 * n - 1] - a[n])*(a[n - 1] - a[0]); // 以 0 为开始选择的区间大小 。
	for(int i=1;i<n;i++) {
		ans = min(ans, (a[i + n - 1] - a[i]) * (a[2 * n - 1] - a[0]));
	}
	cout << ans << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43305984/article/details/89080837