poj3347Kadj Squares【计算几何】

 

Language:
Kadj Squares
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2851 Accepted: 1106

Description

In this problem, you are given a sequence S1S2, ..., 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 S1S2, 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;
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值