poj3347——正方形覆盖(区间覆盖)

题目链接:poj.org/problem?id=3347

In this problem, you are given a sequence S1, S2, ..., Sn of squares of different sizes. The sides of the squares are integer numbers. We locate the squares on the positive x-y quarter of the plane, such that their sides make 45 degrees with x and y axes, and one of their vertices are on y=0 line. Let bi be the x coordinates of the bottom vertex of Si. First, put S1 such that its left vertex lies on x=0. Then, put S1, (i > 1) at minimum bi such that

  • bi > bi-1 and
  • the interior of Si does not have intersection with the interior of S1...Si-1.

 

 

The goal is to find which squares are visible, either entirely or partially, when viewed from above. In the example above, the squares S1, S2, and S4 have this property. More formally, Si is visible from above if it contains a point p, such that no square other than Si intersect the vertical half-line drawn from p upwards.

Input

The input consists of multiple test cases. The first line of each test case is n (1 ≤ n ≤ 50), the number of squares. The second line contains n integers between 1 to 30, where the ith number is the length of the sides of Si. The input is terminated by a line containing a zero number.

Output

For each test case, output a single line containing the index of the visible squares in the input sequence, in ascending order, separated by blank characters.

Sample Input

4
3 5 1 4
3
2 1 2
0

Sample Output

1 2 4
1 3

题目翻译:

在这个问题中,你被赋予一个‎‎序列S‎‎1‎‎, ‎‎S‎‎2‎‎, ..., S ‎‎n‎‎的正方形不同大小.正方形的两侧是整数。我们定位平面正‎‎x‎‎-‎‎y‎‎四分之一上的正方形,这样它们的两侧使用‎‎x‎‎轴和‎‎y‎‎轴进行 45 度,并且其中一个顶点位于‎‎y‎‎=0 线上。让‎‎b‎‎i‎‎是‎‎S‎‎i‎‎的底部顶点的‎‎x‎‎坐标。首先,将‎‎S‎‎1‎‎放在 x =0 上,使其左顶点位于‎‎x‎‎=0 上。然后,将‎‎S‎‎1‎‎,(i‎‎ > 1) 放在至少‎‎b‎‎i‎‎上,这样,‎

  • ‎b‎‎i‎‎ ‎‎ > ‎‎b‎‎i‎‎-1‎‎和‎
  • ‎S‎‎的内部‎‎没有与‎‎S‎‎1‎‎的内部交叉 ...‎‎S‎‎ ‎‎i‎‎-1‎‎.‎

 

 

‎目标是从上面查看时,找到完全或部分可见的方块。在上面的示例中,正方形 S ‎‎1、S‎‎ ‎‎2‎‎和‎‎S‎‎4‎‎具有此属性。更正式的是,‎‎如果 S‎‎i‎‎包含点‎‎p,‎‎则从上面可以看到,这样,除了‎‎S‎‎i‎‎之外,没有任何正方形与从‎‎p‎‎向上绘制的垂直半线相交。‎

‎输入‎

‎输入由多个测试用例组成。每个测试用例的第一行是‎‎n‎‎ (1 = ‎‎n‎‎ = 50),平方数。第二行包含‎‎1‎‎到 30 之间的 n 个整数,其中‎‎i‎‎编号是‎‎S‎‎i‎‎的边的长度。输入由包含零数的行终止。‎

‎输出‎

‎对于每个测试用例,输出一行包含输入序列中可见方块的索引,按升序,由空白字符分隔。

有一种比较简单的做法,记录一下正方形的左右端点和长度,并且投影到x轴,就相当于求区间覆盖了。

不过区间覆盖有两种情况:左边覆盖和右边覆盖。

求当前点的左端点时,可以得出 左端点 L = 前面最大右端点maxR - 两正方形边长之差。

有一个小技巧,在求出左端点后,右端点直接等于左端点+长度乘于2,相当于都扩大sqrt(2)倍,也就是投影到x轴之后的长度,其实都扩大就不影响结果了。

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
struct point{
	int l,r,len;
}p[55];
int main(){
	int n;
	while(~scanf("%d",&n)){
		if(!n) break;
		for(int i=1;i<=n;i++){
			scanf("%d",&p[i].len);
			p[i].l=0;
			for(int j=1;j<i;++j)
				p[i].l=max(p[i].l,p[j].r-abs(p[j].len-p[i].len));
				//当前左边的端点等于前面所有最大的右端点减去边长之差 
			p[i].r=2*p[i].len+p[i].l;
			//相当于同时扩大了sqrt(2)倍 
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<i;++j)//找左边 
				if(p[i].l<p[j].r&&p[i].len<p[j].len)//左边点与当前点有重叠部分 
					p[i].l=p[j].r;
			for(int j=i+1;j<=n;++j)//找右边 
				if(p[i].r>p[j].l&&p[i].len<p[j].len)//右边点与当前点有重叠部分 
					p[i].r=p[j].l;
		}
		int first=1;
		for(int i=1;i<=n;i++){
			if(p[i].l<p[i].r&&first){
				printf("%d",i);
				first=0;
				continue;
			}
			if(p[i].l<p[i].r&&!first)
				printf(" %d",i);
		}
		printf("\n");			
	}
	
	return 0;
}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值