天天看点

2016-2017 CT S03E05: Codeforces Trainings Season 3 Episode 5 J

链接:戳这里

题意:

二维平面上给出n(奇数)个点,要求找出能包围n/2+1个点的面积最小的矩形,该矩形平行于坐标轴

思路:

枚举矩形的width的边界,然后把框起来的点按y排序,枚举当前y,然后找到Y[I+m-1]的位置,统计最小面积

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n;
struct point{
    int x,y;
    point(int x=0,int y=0):x(x),y(y){}
}s[330],p[330];
bool cmp1(point a,point b){
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
bool cmp2(point a,point b){
    if(a.y==b.y) return a.x<b.x;
    return a.y<b.y;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d%d",&s[i].x,&s[i].y);
        if(n==1) {
            printf("0\n");
            continue;
        }
        int m=n/2+1;
        sort(s+1,s+n+1,cmp1);
        int ans=1e9;
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                int num=0;
                for(int k=1;k<=n;k++){
                    if(s[k].x>=s[i].x && s[k].x<=s[j].x) p[++num]=s[k];
                }
                sort(p+1,p+num+1,cmp2);
                int X1=s[i].x;
                int X2=s[j].x;
                for(int k=1;k<=num-m+1;k++){
                    int Y1=p[k].y;
                    int Y2=p[k+m-1].y;
                    ans=min(ans,(X2-X1)*(Y2-Y1));
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
           

继续阅读