java jurisdiction_Jurisdiction Disenchantment(二维尺取)

该博客讨论了一种算法问题,涉及在一个平面直角坐标系中,通过选取一个轴对齐的矩形来包含超过一半的点。给定n个点的坐标,目标是找到能覆盖超过n/2个点的最小矩形面积。解决方案是通过排序和二维尺取法来确定最优矩形的边界,从而找到最小面积。
摘要由CSDN通过智能技术生成

题目描述

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/2+1 个点,求矩形最小面积。

思路

二维尺取,把 n 个点按 x 降序排序, 枚举 x 的边界,维护在这个边界中 n/2+1 个点的最小矩形面积。

代码

#include

using namespace std;

const int maxn = 300;

const int inf = 0x7fffffff;

int T, n;

struct node {

int x, y;

void read() {

scanf("%d%d", &x, &y);

}

inline bool operator

return y < p.y;

}

} a[maxn], b[maxn];

bool cmp(node a, node b) {

return a.x < b.x || a.x == b.x && a.y < b.y;

}

int main() {

scanf("%d", &T);

while (T--) {

scanf("%d", &n);

for (int i = 1; i <= n; ++i) {

a[i].read();

b[i] = a[i];

}

sort(a + 1, a + 1 + n, cmp);

int ans = INT_MAX, m = n / 2 + 1;

for (int i = 1; i <= n; ++i) {

for (int j = i; j <= n; ++j) {

int lx = a[i].x, rx = a[j].x;

int cnt = 0;

for (int k = 1; k <= n; ++k) {

if (a[k].x < lx || a[k].x > rx) continue;

b[++cnt] = a[k];

}

if (cnt >= m) {

sort(b + 1, b + 1 + cnt);

for (int k = 1; k <= cnt - m + 1; ++k) {

int ly = b[k].y, ry = b[k + m - 1].y;

ans = min(ans, (rx - lx) * (ry - ly));

}

}

}

}

printf("%d\n", ans);

}

return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值