[POJ 3347] Kadj Squares【计算几何】

  • 2017-12-13
  • 0
  • 0

Problem:

Time Limit: 2000MS Memory Limit: 65536K

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

i) bi > bi-1 and

ii) 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

Source

Tehran 2006

Solution:

本题实际上并不复杂,但是细节较多,实现时需要理清思路。

首先,按顺序枚举每个正方形,将其与之前的每一个正方形贴紧。由于此时可能与其他正方形有交集,所以取这些值中最大的为合法的坐标最小值。

计算方法如下:

然后对于每个正方形,枚举所有比它大的正方形,计算它被该正方形覆盖的区间,以差分形式存储,最后从左到右扫一遍进行累加即可。

对于正方形S[j]覆盖正方形S[i](其中a[j]>a[i])的区间为[ b[i] - a[i], b[i] + a[i] )。

示意图如下:

ps: 注意细节!

第一,差分数组要记录每段长度为1的区间是否被覆盖,而不能记录每个点;

第二,枚举更大的正方形时,注意其左右端点超过当前正方形的边界的情况,从而导致数据无效。

Code: O(n2ST), S为最大边长 [172K, 0MS]

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

int n, a[55], b[55];
int d[3005];  // The max x-coordinate is 3000, which equals to 50 * S

int main(){
	while(scanf("%d", &n) && n){
		for(register int i = 1; i <= n; i++) scanf("%d", a + i);
		// Amplify the length to sqrt(2) times as origin when manipulating below
		for(register int i = 1; i <= n; i++){
			b[i] = a[i];  // The square cannot get across the y-axis
			for(register int j = 1; j < i; j++){
				int cur = b[j] + 2 * min(a[i], a[j]);
				b[i] = max(b[i], cur);
			}
			// For each left square, try to put current one at the min position
			// The min legal position is the max of former positions
		}
		bool fir = 1;  // Control output format
		for(register int i = 1; i <= n; i++){
			for(register int j = b[i] - a[i]; j < b[i] + a[i]; j++) d[j] = 0;
			// Initialize the array d[], d[i] controls a region [i, i + 1)
			for(register int j = 1; j <= n; j++){
				if(a[j] <= a[i]) continue;  // Only larger square can cover smaller square
				int inl = max(b[i] - a[i], b[j] - a[j]), inr = min(b[i] + a[i], b[j] + a[j]);
				// Covered region might get across [ b[i] - a[i], b[i] + a[i] ), so control it by max/min
				if(inl < inr) d[inl]++, d[inr]--;
				// Use the method of "difference" to record the coverage
			}
			int presum = 0;
			for(register int j = b[i] - a[i]; j < b[i] + a[i]; j++){
				presum += d[j];
				if(presum == 0){
					if(fir) fir = 0; else printf(" ");
					printf("%d", i);
					break;  // Once there is a point without coverage, output this square
				}
			}
		}
		puts("");
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址: darkleafin.cf

常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai