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 }
时间: 2024-10-25 10:25:53

POJ3347 Kadj Squares(计算几何&区间覆盖)的相关文章

poj3347 Kadj Squares (计算几何)

D - Kadj Squares Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description In this problem, you are given a sequence S1, S2, ..., Sn of squares of different sizes. The sides of the squares are integer numb

POJ 3347 Kadj Squares(计算几何)

传送门 Kadj Squares Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2937 Accepted: 1151 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

[poj3347]Kadj Squares

题目大意:斜45度摆放的正方形,靠左摆放,但需与x轴接触,求按顺序摆放,从上方可以观察到的正方形序号. 解题关键:因为题目没让输出与边长有关的东西,所以可以直接将边长设为左端点到中心的距离,来消除误差.求出每个正方形的左端点和右端点之后,然后dp一下求出每个正方形最左端可以被看到的位置和最右端可以被看到的位置.判断一下即可出答案. #include<cstdio> #include<cstring> #include<algorithm> #include<cst

ural Minimal Coverage (区间覆盖)

http://acm.timus.ru/problem.aspx?space=1&num=1303 给出一些区间,选择尽量少的区间能覆盖到[0,m]. 小白p154,典型的区间覆盖问题.一直在想怎么dp.. 首先预处理,先按左端点从小到大排序,若左端点相同右端点从大到小排序,若区间x完全包含y,按照贪心的思想,y是没有意义的,有大区间可以选何必选择小区间.处理完事之后各个区间满足a1 <= a2 <= a3....且b1 <= b2 <= b3... 这样找到第一个覆盖0的

codeforces Gym 100187F F - Doomsday 区间覆盖贪心

F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F Description Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will

算法模板——线段树3(区间覆盖值+区间求和)

实现功能——1:区间覆盖值:2:区间求和 相比直接的区间加,这个要注重顺序,因为操作有顺序之分.所以这里面的tag应该有个pushup操作(本程序中的ext) 1 var 2 i,j,k,l,m,n,a1,a2,a3,a4:longint; 3 a,b,d:array[0..100000] of longint; 4 function max(x,y:longint):longint;inline; 5 begin 6 if x>y then max:=x else max:=y; 7 end;

算法模板——线段树4(区间加+区间乘+区间覆盖值+区间求和)

实现功能——1:区间加法 2:区间乘法 3:区间覆盖值 4:区间求和 这是个四种常见线段树功能的集合版哦...么么哒(其实只要协调好三种tag的关系并不算太难——前提是想明白了线段树的工作模式) 代码长度几经修改后也大为缩水 还有!!!——通过BZOJ1798反复的尝试,我的出来一个重要结论——尽量减少pushup操作的不必要使用次数,对于程序提速有明显的效果!!! 1 type vet=record 2 a0,a1:longint; 3 end; 4 var 5 i,j,k,l,m,n,a1,

HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便,我们将小时换成分钟,那么一天24小时,总共有1440分钟.顾我就可以把一天里的任意HH:MM时间换成分钟.就这样一天的时间就变成[0,1440]区间了. 因为所给的活动最多是5*10^5,如果把活动的时间在线段[0,1440]都修改,那么时间的复杂度最坏是O(5*10^5*1440). (1)方法一

POJ3171 Cleaning Shifts DP,区间覆盖最值

题目大意,N个区间覆盖[T1,T2]及对应的代价S,求从区间M到E的全部覆盖的最小代价是多少. (1 <= N <= 10,000),(0 <= M <= E <= 86,399). 思路是DP,首先将每个区间按照T2从小到大排序,设dp(k)为从m覆盖到k所需最小代价,则有 dp(T2[i]) = min(dp(T2[i]), {dp(j) + Si,  T1[i] - 1<=j <= T2[i]}),对于 {dp(j) + Si,  T1[i] - 1<