gym Jurisdiction Disenchantment

问题 J: Jurisdiction Disenchantment

http://codeforces.com/gym/101116/attachments

The Super League of Paragons and Champions (SLPC) has been monitoring a plot by a corrupt politician to steal an election. In the past week, the politican has used a mind-control technique to enslave the n representatives responsible for choosing the election’s winner. Luckily, the SLPC has managed to recruit you and hence has access to your power to break mind-control. You are able to break mind-control in an axis-aligned rectangle. Unfortunately, your power comes at a steep cost; you get a headache the next day proportional to the size of the rectangle. You do not even want to risk or think about what would happen if you tried to use your power multiple times in one day.
You have done your research and you know the position that each representative will be standing when the votes are about to be cast. You need to free enough representatives to prevent the politician from having a majority (strictly more than one-half) vote. What is the area of the smallest axis-aligned rectangle that you can affect to do this?

 

输入

The first line of input contains a single integer T (1 ≤ T ≤ 10), the number of test cases. The first line of each test case contains a single integer n (1 ≤ n ≤ 299, n is odd), the number of representatives. Each of the next n lines of input contains two integers, denoting the x and y coordinates of a representative. It is guaranteed that all coordinates are between −10,000 and +10,000.

 

输出

For each test case, output a single line containing the area of the smallest axis-aligned rectangle containing more than n/2 of the representatives.

 

样例输入

复制样例数据

2
1
1 1
3
0 0
1 4
3 2

样例输出

0
4

提示

In the first case, a rectangle containing a single point has an area of 0.
In the second test case, the rectangle needs to include at least two points. There are two smallest possible
rectangles; one includes (0, 0) and (1, 4) and the other includes (1, 4) and (3, 2). In either case, the area is
4.

题意是给定n个点(n为奇数),找出可以覆盖n/2+1个点的最小的矩形面积

可以先按照x从左到右排序,然后找在一段区间[i,j]的点是否满足n/2+1个,如果满足的话,就可以计算面积,然后取最小值,

这里类似于一个包含num个点的窗口左右移动,然后枚举左右边界,找最小值。(先按y排序,然后窗上下移动也是可以的,其实就是固定一个方向,找另一个方向)

因为这一段长度已经确定,所以就看高度,把这段区间内的点按照y从下往上排序。

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e5+5;
struct node
{
    int x,y;
}p[maxn],pp[maxn];
bool cmp1(node a,node b)//按x排序
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
bool cmp2(node a,node b)//按y排序
{
    if(a.y==b.y)
        return a.x<b.x;
    return a.y<b.y;
}
int ans;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        ans=INF;
        for(int i=0;i<n;i++)
            scanf("%d%d",&p[i].x,&p[i].y);
        if(n==1)
        {
            printf("0\n");
            continue;
        }

        sort(p,p+n,cmp1);//先按x从左到右排序
        int num=n/2+1;//要包含n/2+1个
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                int tot=0;
                int x1=p[i].x,x2=p[j].x;
                for(int k=0;k<n;k++)//记录在p[i].x~p[j].x之间的点
                {
                    if(p[k].x>=x1&&p[k].x<=x2)
                        pp[tot++]=p[k];
                }
                if(tot>=num)
                {
                    sort(pp,pp+tot,cmp2);//按y从低到高排序
                    for(int k=0;k<=tot-num;k++)//k是左边,为了满足区间内的点>=num,右边至少要有num个点 类似于一个宽为num个点的窗口滑动
                    {
                        int y1=pp[k].y,y2=pp[k+num-1].y;//y2是num个点的右边界,为了满足区间内的点>=num,左边至少要有num个点
                        ans=min(ans,(x2-x1)*(y2-y1));
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值