C. Photo of The Sky

  • 题目链接:http://codeforces.com/contest/1013/problem/C
  • 题意:给你2*n 个整数,要求你把这2*n个数分为两组,一组为x坐标,一组为y坐标,每组n个数。任你选一种组合方式,让其组成n个点,使得包含所有xy坐标表示的点的矩形(边与坐标轴平行)的最小面积。输出最小面积
  • 思路:包含两种情况:
    • 一 :最大值和最小值在同一组中:那么一条边就确定为max(x)-min(x),接着求另一条边。要求 这条边长度最小。由已知,每组有n个数,由于第一条边已经确定,其余各点都在该边范围内。所以只要求出另一组中的max(y)-min(y)即可。将2*n个数sort以后,只需要找出最小的连续n个数的max(y)-min(y)就行,答案即为(max(x)-min(x))* min(max(y)-min(y))
    • 二:最大值和最小值不在同一组中:假设最小值在X组中,最大值在Y组中。因为X组的大小为n,所以X组中的最大值max(x) >= a[n];又因为Y组的大小为n,所以Y组中的最小值min(y) <= a[n+1]。所以 (max(x) - min(x)) * (max(y) - min(y)) >= (a[n]-a[1])(a[2*n]-a[n+1])。因此面积的最小值等于(a[n]-a[1])(a[2*n]-a[n+1])

#include <bits/stdc++.h>
#define pi acos(-1)
#define fastcin ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 0x3f3f3f3f;       // 不能加负号!!!
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;//4e18 ~= 2^62
const int maxn =100000 + 10;
const LL mod = 1e9+7;

LL a[maxn*2];

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=1; i<=2*n; i++){
        scanf("%I64d", &a[i]);
    }
    sort(a+1, a+1+2*n);
    LL ans = 0;
    ans = (a[n]-a[1])*(a[2*n]-a[n+1]);
    LL tmp = 0;
    for(int i=2; i<=n; i++){
        tmp = a[i+n-1] - a[i];
        ans = min(ans, tmp*(a[2*n]-a[1]));
    }
    //printf("%I64d %I64d %I64d %I64d\n", a[n], a[1], a[2*n], a[n+1]);
    cout << ans << endl;
}

猜你喜欢

转载自blog.csdn.net/qq_37352710/article/details/81412586
sky