Finding the Rectangle
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. 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
POJ Founder Monthly Contest – 2008.07.27, windy7926778
|
题意:平面上有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;
}