[平面几何][思维]Kadj Squares POJ3347

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

题意: 给出n个正方形的边长,按给出顺序以旋转45°的形式排放在坐标轴上,并且要求尽可能紧凑,如上图所示。询问最终从上方能看到的正方形编号。

分析: 细节题。对于每个正方形边长可以乘根号2,这样可以避免接下来出现浮点数。按顺序枚举每一个正方形,若当前正方形边长a[i]小于上一个正方形边长a[i-1],那么这个正方形的位置x[i] = x[i-1] + 2*a[i],2*a[i]是乘根2后的对角线长度,若a[i] >= a[i-1]且a[i] <= 2*a[i-1],此时当前的正方形还是可以和上一个正方形贴着的,其位置x[i] = x[i-1] + 2*a[i-1],2*a[i-1]是上一个正方形对角线长度,最后一种情况a[i] > 2*a[i-1],这时候正方形位置并不确定,我们可以枚举之前所有的正方形,假设它是跟当前正方形挨着,计算得出可能的横坐标t,对于所有的t取一个最大值就是当前正方形的坐标,因为当前这个正方形一定会挨着前i-1个正方形中的某一个,如果取了非最大的坐标那么就会和最大的坐标产生冲突,所以一定要选横坐标最大的点。

确定了每个正方形的坐标后它们各自能伸展的区间也确定了,l[i] = x[i]-a[i],r[i] = x[i]+a[i],得到区间后可以用类似染色的方法刷数组,遍历每个正方形的l~r一遍,如果当前位置的grade小于这个正方形的grade就覆盖掉这个位置,grade也就是正方形的边长,同时每个正方形的区间长度还需要扩大两倍,防止刷数组时出现某个颜色被挤掉的情况,例如给出三个边长为2,1,1的正方形,它们覆盖的区间分别为[0, 4], [3, 5], [5, 7],中间的正方形被遮挡住了,实际上是不会被遮住的。

染完色后再扫一遍数组输出一次不同的颜色即可。

具体代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int a[55], x[55], l[55], r[55];
struct node
{
	int color;
	int grade;
}node[100005];
bool vis[55];

signed main()
{
	int n;
	while(~scanf("%d", &n))
	{
		if(n == 0)
			break;
		for(int i = 1; i <= n; i++)//小技巧,边长都乘根2 
		{
			scanf("%d", &a[i]);
			vis[i] = x[i] = l[i] = r[i] = 0;
		}
		for(int i = 0; i <= 10000; i++)
			node[i].color = node[i].grade = 0;
		x[1] = a[1];
		l[1] = 0;
		r[1] = 2*a[1];
		for(int i = 2; i <= n; i++)
		{
			if(a[i] < a[i-1])
				x[i] = x[i-1]+2*a[i];
			else if(a[i] >= a[i-1] && a[i] <= 2*a[i-1])
				x[i] = x[i-1]+2*a[i-1];
			else
			{
				for(int j = 1; j <= i-1; j++)//现在不确定挨着哪个正方形,需要枚举出来
				{
					if(a[i] < a[j])
						x[i] = max(x[i], x[j]+2*a[i]);
					else
						x[i] = max(x[i], x[j]+2*a[j]);
				} 
			}
			l[i] = x[i]-a[i], r[i] = x[i]+a[i];
		}
		for(int i = 1; i <= n; i++)
		{
			for(int j = 2*l[i]; j <= 2*r[i]; j++)//区间扩大一倍 
			{
				if(a[i] >= node[j].grade)
				{
					node[j].color = i;
					node[j].grade = a[i];
				}
			}
		}
		for(int i = 0; i <= 10000; i++)
		{
			if(!vis[node[i].color] && node[i].color)
			{
				vis[node[i].color] = true;
				printf("%d ", node[i].color);
			}
		}
		puts("");
		
	}
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: `squares.append()`是Python语言中列表对象的一个方法,用于在列表末尾添加一个元素。例如,如果你有一个名为"squares"的列表,并且想在其末尾添加数字9,你可以使用以下代码: ``` squares.append(9) ``` 这将在列表末尾添加数字9。 ### 回答2: 在Python中,squares.append()是一个列表方法,用于向列表中添加元素。该方法接受一个参数,即要添加的元素,并将该元素添加到列表的末尾。 例如,假设我们有一个空列表squares。我们可以使用以下代码将一个元素添加到列表中: squares = [] squares.append(4) 现在,squares列表将包含一个元素,即数字4。我们可以进一步使用append()方法来添加更多的元素到列表中。例如: squares.append(9) squares.append(16) squares.append(25) 现在,squares列表将依次包含数字4、9、16和25。 总结而言,squares.append()是一个有用的列表方法,它允许我们在Python中向列表中添加元素。无论列表是否为空,我们都可以使用append()方法将元素添加到列表的末尾。 ### 回答3: `python squares.append`是Python语言中的一个方法,用于将一个元素追加到列表(list)中。在使用该方法时,需要先创建一个列表对象,并且可以在括号内指定要追加的元素。 例如,以下是一个使用`squares.append`的示例: ```python squares = [] # 创建一个空的列表对象 # 使用append方法将元素依次追加到列表中 squares.append(1) squares.append(4) squares.append(9) squares.append(16) print(squares) # 输出结果为:[1, 4, 9, 16] ``` 在上述示例中,我们首先创建了一个空的列表对象`squares`。然后,使用`squares.append`方法依次向列表中追加了四个元素,分别是1、4、9和16。最后,通过`print(squares)`将列表内容打印输出,结果为`[1, 4, 9, 16]`。 总而言之,`python squares.append`是用于将一个元素追加到列表中的Python方法,可以方便地扩充列表的内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值