poj 3681 Finding the Rectangle 尺取法解最小矩形覆盖问题

题意:

平面上有n个点,现在要求一个面积最小的矩形能完全覆盖其中的m个点(边界不算)。

分析:

求满足某个性质的最小区间的问题尺取法比二分还要高效,这题可以在x上暴力枚举,在y上用尺取法(在x,y上都用尺取法是不对的)。

代码:

//poj 3681
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,ans;
struct P
{
	int x,y;
}pnt1[256],pnt2[256];

int cmp1(P a,P b)
{
	return a.x<b.x;
}

int cmp2(P a,P b)
{
	return a.y<b.y;
}
int vis[256];

void judge(int lx,int rx)
{
	int y[256],u=0,d=0;
	for(int i=0;i<n;++i)
		if(pnt2[i].x>=lx&&pnt2[i].x<=rx){
			y[u++]=pnt2[i].y;
			if(u-d==m){
				ans=min(ans,(y[u-1]-y[d]+2)*(rx-lx+2));
				++d;
			}
		}
}

int main()
{
	int cases;
	scanf("%d",&cases);
	while(cases--){
		scanf("%d%d",&n,&m);
		for(int i=0;i<n;++i){
			scanf("%d%d",&pnt1[i].x,&pnt1[i].y);
			pnt2[i]=pnt1[i];
		}
		sort(pnt1,pnt1+n,cmp1);
		sort(pnt2,pnt2+n,cmp2);
		ans=INT_MAX;
		int l,r;
		for(int i=0;i<n;++i)
			for(int j=i;j<n;++j)
				judge(pnt1[i].x,pnt1[j].x);
		printf("%d\n",ans);
	}
	return 0;
} 
时间: 2024-11-03 08:35:53

poj 3681 Finding the Rectangle 尺取法解最小矩形覆盖问题的相关文章

POJ 3020 Antenna Placement(二分图建图训练 + 最小路径覆盖)

题目链接:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6692   Accepted: 3325 Description The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobi

poj 2594Treasure Exploration(有向图路径可相交的最小路径覆盖)

1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 #define N 505 6 using namespace std; 7 8 int g[N][N]; 9 int n, m; 10 int vis[N], linker[N]; 11 bool dfs(int u){ 12 for(int i=1; i<=n; ++i) 13 if

POJ - 2566 Bound Found(尺取法+前缀和)

题目链接:http://poj.org/problem?id=2566 题意:给定一个序列(n个整数)和一个整数k(m个),求出这个序列的一个子串,使之和的绝对值与k的差最小. 尺取法的题目有两个特性: 1. 所求的序列是一个连续的序列,这样才能将序列抽象成一个头和一个尾来描述. 2. 头尾枚举的序列满足某种单调的性质,这样才能进行尺取的操作. 这个序列是一个随意的序列,不可能直接对其进行操作,先要预处理下,进行前缀和操作,把对应的值和标记放在同一个pair. 然后根据前缀和的值进行排序,这样就

poj 2566 Bound Found(尺取法 好题)

Description Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: "But I want to use feet, not meters!"). Each signal seems to

poj 2100 Graveyard Design(尺取法)

Description King George has recently decided that he would like to have a new design for the royal graveyard. The graveyard must consist of several sections, each of which must be a square of graves. All sections must have different number of graves.

POJ 3061 Subsequence (二分||尺取法)

A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements of the sequen

POJ 1442 Air Raid(DAG图的最小路径覆盖)

题意: 有一个城镇,它的所有街道都是单行(即有向)的,并且每条街道都是和两个路口相连.同时已知街道不会形成回路. 可以在任意一个路口放置一个伞兵,这个伞兵会顺着街道走,依次经过若干个路口. 问最少需要投放几个伞兵,使得每个路口都被伞兵拜访过.并且要满足每个路口只能被一个伞兵拜访过. 思路: 裸DAG图的最小路径覆盖. DAG图的最小路径覆盖数 = 节点数 - 二分图最大匹配 代码: vector<int> graph[125]; int cx[125],cy[125]; bool bmask[

POJ 1422 &amp;&amp; ZOJ 1525 --Air Raid【二分图 &amp;&amp; 最小路径覆盖】

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7451   Accepted: 4434 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

【最小矩形面积覆盖:凸包+旋转卡壳】UVA 10173 Smallest Bounding Rectangle

[最小矩形面积覆盖:凸包+旋转卡壳]UVA 10173 Smallest Bounding Rectangle 题目链接:UVA 10173 Smallest Bounding Rectangle 题目大意 给你n个点,求能够覆盖所有点集的最小矩形面积. 笔者的第2道凸包题目,凸包 + 旋转卡壳,实现点集的最小矩形面积覆盖问题 ">=0"写成"<=0"坑了我一下午!QAQ 说一下思路 ①Graham's Scan法构建凸包,时间复杂度O(nlogn) ②