首页 > 代码库 > POJ3347 Kadj Squares(计算几何&区间覆盖)

POJ3347 Kadj Squares(计算几何&区间覆盖)

题目链接:

  http://poj.org/problem?id=3347

题目描述:

Kadj Squares
 

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

 

题目大意:

  给出正方形边长,按顺序无缝如图摆放,问从上面看哪些矩形可以被看到

思路:

  首先我们把正方形扩大根号二倍,这样他们与x轴相交的点就是整点了

  每个点坐标为他之前的坐标加上两个正方形对角线的最大值

  然后对于每个正方形

  判断在他左侧所有比他边长大的正方形的对角线的最大坐标(r)

  和在他右侧所有比他边长大的正方形的对角线的最小坐标(l)

  是否覆盖当前正方形

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int N = 55;
 8 const int INF = 0x3f3f3f3f;
 9 
10 int d[N], x[N], n;
11 
12 int main() {
13     x[0] = d[0] = 0;
14     while (cin >> n&&n) {
15         for (int i = 1; i <= n; ++i) {
16             scanf("%d", &d[i]);
17             int best = d[i];
18             for (int j = 1; j < i; ++j)
19                 best = max(best, x[j] + 2 * (d[j] > d[i] ? d[i] : d[j]));    //处理与x轴交点坐标
20             x[i] = best;
21         }
22         for (int i = 1; i <= n; ++i) {
23             int r = 0, l = INF;    // r为左边线段最右点 l为右边线段最左点
24             for (int j = 1; j < i; ++j)if (d[j] > d[i])r = max(r, x[j] + d[j]);    //只考虑比第i个正方形大的
25             for (int j = i + 1; j <= n; ++j)if (d[j] > d[i])l = min(l, x[j] - d[j]);
26             if (r >= x[i] + d[i] || l <= x[i] - d[i] || r >= l)continue;        //如果全覆盖或者左右线段相交则说明不能看见
27             printf("%d ", i);
28         }
29         printf("\n");
30     }
31 }

 

POJ3347 Kadj Squares(计算几何&区间覆盖)