1. 程式人生 > >POJ3347 Kadj Squares(計算幾何&區間覆蓋)

POJ3347 Kadj Squares(計算幾何&區間覆蓋)

ica nsis -s ber pro ins ascend char rst

題目鏈接:

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

題目描述:

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

  • bi > bi-1 and
  • the interior of Si does not have intersection with the interior of S1...S
    i-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

題目大意:

  給出正方形邊長,按順序無縫如圖擺放,問從上面看哪些矩形可以被看到

思路:

  首先我們把正方形擴大根號二倍,這樣他們與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(計算幾何&區間覆蓋)