Language:
Kadj Squares
Description 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
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 |
题意:给出每个正方形的边长每个正方形都旋转45度尽量靠左放之后从上往下看能够看到几个正方形。
解题思路:先计算放好后每个正方形左右端点的位置先计算左端的位置依次遍历要放的正方形前面已经放的正方形的位置算出左端点的最大值即为当前要放的左端点值
当该正方形的边长小于前一个时有左端点为A[j].l+sqrt(2)/2*A[j].len+A[i].len*sqrt(2)/2;如果直接计算的话可能产生浮点数误差所以可以假设将他们都乘以sqrt(2)即可消除该误差
最后通过枚举更新每个正方形的左右端点即可。当一个正方形的左端点值大于等于右端点值时即该正方形不可见。
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
struct Node{
int l,r,len;
}A[55];
int MAX(int a,int b){
return a>b?a:b;
}
int main()
{
int n,i,j,k;
while(scanf("%d",&n),n){
for(i=1;i<=n;++i){
scanf("%d",&A[i].len);
int temp;A[i].l=0;
for(j=1;j<i;++j){
if(A[i].len<A[j].len)temp=A[j].l+A[j].len+A[i].len;
else temp=A[j].l+3*A[j].len-A[i].len;
A[i].l=MAX(temp,A[i].l);
}
A[i].r=A[i].l+2*A[i].len;
}
for(i=2;i<=n;++i){
for(j=1;j<i;++j){
if(A[j].len<A[i].len&&A[j].r>A[i].l){
A[j].r=A[i].l;
}
else if(A[j].len>A[i].len&&A[j].r>A[i].l)
A[i].l=A[j].r;
}
}
for(i=1;i<=n;++i){
if(A[i].l>=A[i].r)continue;
printf("%d ",i);
}
printf("\n");
}
return 0;
}