1013 C. Photo of The Sky

Posted mch5201314

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1013 C. Photo of The Sky相关的知识,希望对你有一定的参考价值。

传送门

[http://codeforces.com/contest/1013/problem/C]

题意

输入一个n代表n颗星星,输入2n个数,其中任意两个数代表一颗行星的坐标,问你把n个星星围起来的最小矩形面积。

思路

先对2n 个数小到大排序,因为矩形是要求把这n个点框住的,所以稍微想一下不难得到:S=(max(x)?min(x))?(max(y)?min(y))
于是我们将原问题这样转化:

给你2n个数,把这2n个数放在两个集合当中,每个集合的元素个数为n,设这两个集合分别为X,Y,
求min((Xmax?Xmin)?(Ymax?Ymin))
接下来我们来讨论,如何分放集合。

考虑如果最大数a2n与最小数a1如果在同一个集合X,那么现在我们要求min(Ymax?Ymin)。

考虑一下怎样的情况才会有min(Ymax?Ymin)的情况出现。假设Ymin在a中为ai,那么Ymax在a中一定为ai+n?1,为什么呢?

如果Ymax在ai+1?>ai+n?2之间,那么Y集合里面的元素个数就没有要求的n个了,不满足。

如果Ymax在ai+n?>a2n?1之间,那么显然可以在满足元素个数为n的情况下使Ymax最小。

所以在最大数a2n与最小数a1如果在同一个集合X时,答案的值为:
Ans=min(a[2n]?a[1])?(a[i+n?1]?a[i])2≤i≤n
那么还有一种情况就是最大数a2n与最小数a1不在同一个集合当中,与上面类似的讨论不难得到最后的结果唯一:
Ans=(a[2n]?a[n+1])?(a[n]?a[1])
所以我们最后的结果在上面两者之间取最小即可。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=200010;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("in.txt","r",stdin);
    int n,i;
     ll a[maxn];
    while(cin>>n){
        memset(a,0,sizeof(a));
        for(i=1;i<=2*n;i++)
        cin>>a[i];
        sort(a+1,a+2*n+1);
        ll ans=(a[2*n]-a[n+1])*(a[n]-a[1]);
        for(i=2;i<=n;i++)
        ans=min(ans,(a[2*n]-a[1])*(a[n+i-1]-a[i]));
        cout<<ans<<endl;
    }
    return 0;
}







以上是关于1013 C. Photo of The Sky的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 1012A Photo of The Sky

Codeforces Round #500 (Div. 2) C Photo of The Sky

Number of Airplanes in the Sky

391. Number of Airplanes in the Sky

[LintCode] Number of Airplanes in the Sky

lintcode-medium-Number of Airplanes in the Sky