POJ3681-Finding the Rectangle

Finding the Rectangle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3375 Accepted: 1020

Description

Given N point on the x-y coordinates, you are to find the rectangle of the smallest area which covers no less than M points. The coordinates of the four vertices must be integers and the sides must be parallel to the axes. If one point lies on the sides of the rectangle, it is not considered as being covered. 

Input

The first line of input is the number of test case.
The first line of each test case contains two integers, N and M (1 ≤ M ≤ N ≤ 200).
The next N line each contain two integers xiyi (1 ≤ xiyi ≤ 10,000) describing the points. 
There is a blank line before each test case.

Output

For each test case output the smallest area in a separate line.

Sample Input

5

2 1
200 6
100 5

2 2
200 6
100 5

2 1
1 1
1 1

2 2
1 1
1 1

8 5
5 7
6 6
6 8
7 5
7 9
8 6
8 8
9 7

Sample Output

4
306
4
4
20

Source



题意:平面上有n个点,要用一个面积最小的矩形能完全覆盖其中的m个点

解题思路:将所有点按纵坐标从小到大排序,然后暴力枚举两个点作为矩形的横坐标,然后再找出再这个点之间的点,因为纵坐标是有序的,所以可以用尺取法解决


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m;
struct node
{
	int x, y;
}a[300], b[300];

bool cmp(node a, node b)
{
	if (a.y != b.y) return a.y<b.y;
	else return a.x<b.x;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y);
		sort(a + 1, a + 1 + n, cmp);
		int mi = INF;
		for (int i = 1; i <= n; i++)
		{
			for (int j = i; j <= n; j++)
			{
				int x1 = min(a[i].x,a[j].x) - 1, x2 = max(a[i].x,a[j].x) + 1;
				int head = 0, rear = 0, sum = 0;
				for(int k=1;k<=n;k++)
				{
					if (a[k].x <= x1 || a[k].x >= x2) continue;
					b[rear++]=a[k];
					sum++;
					while (sum >= m)
					{
						mi = min(mi, (x2 - x1)*(b[rear-1].y + 1 - b[head].y + 1));
						head++;
						sum--;
					}
				}
			}
		}
		printf("%d\n", mi);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值