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
题目翻译:
在这个问题中,你被赋予一个序列S1, S2, ..., S n的正方形不同大小.正方形的两侧是整数。我们定位平面正x-y四分之一上的正方形,这样它们的两侧使用x轴和y轴进行 45 度,并且其中一个顶点位于y=0 线上。让bi是Si的底部顶点的x坐标。首先,将S1放在 x =0 上,使其左顶点位于x=0 上。然后,将S1,(i > 1) 放在至少bi上,这样,
- bi > bi-1和
- S的内部没有与S1的内部交叉 ...S i-1.
目标是从上面查看时,找到完全或部分可见的方块。在上面的示例中,正方形 S 1、S 2和S4具有此属性。更正式的是,如果 Si包含点p,则从上面可以看到,这样,除了Si之外,没有任何正方形与从p向上绘制的垂直半线相交。
输入
输入由多个测试用例组成。每个测试用例的第一行是n (1 = n = 50),平方数。第二行包含1到 30 之间的 n 个整数,其中i编号是Si的边的长度。输入由包含零数的行终止。
输出
对于每个测试用例,输出一行包含输入序列中可见方块的索引,按升序,由空白字符分隔。
有一种比较简单的做法,记录一下正方形的左右端点和长度,并且投影到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;
}