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 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 ,表示有 n 个正方形,然后n个整数,表示正方形的的边长,将正方形旋转45°,没个正方形都尽量靠左摆放,但是横坐标不能超过0,问的是从上往下看,能够看到几个正方形。

解题思路:

第一步:首先我们要做的事是将这n个正方形的左右端点的横坐标求出来,咋求呢,其实就是每次求下标

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 55;
const double eps = 1e-8;
const double sq2 = sqrt(2.0);
struct Point
{
    double left, right, len;
}p[MAXN];
int main()
{
    int n;
    while(cin>>n,n)
    {
        for(int i=1; i<=n; i++)
            scanf("%lf",&p[i].len);
        p[1].left = 0;
        for(int i=1; i<=n; i++)
        {
            double l = 0;
            for(int j=1; j<i; j++)
            {
                l = max(l, p[j].right-fabs(p[i].len-p[j].len)/sq2);
            }
            p[i].left = l;
            p[i].right = p[i].left + p[i].len*sq2;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<i; j++)
            {
                if(p[i].len>p[j].len && p[i].left<p[j].right)
                    p[j].right = p[i].left;
                if(p[i].len<p[j].len && p[i].left<p[j].right)
                    p[i].left = p[j].right;
            }
        }
        for(int i=1; i<=n; i++)
        {
            if(p[i].right-p[i].left > eps)
                printf("%d ",i);
        }
        puts("");
    }
    return 0;
}
时间: 2024-09-30 14:13:00

POJ 3347 Kadj Squares(计算几何)的相关文章

POJ 3347 Kadj Squares (计算几何+线段相交)

题意:从左至右给你n个正方形的边长,接着这些正方形都按照旋转45度以一角为底放置坐标轴上,最左边的正方形左端点抵住y轴,后面的正方形依次紧贴前面所有正方形放置,问从上方向下看去,有哪些正方形是可以被看到的(如图) 题解:首先我们找到每个正方形左右端点的坐标转化为一条线段,接着我们寻找哪些线段被其他某些条线段覆盖,这些被覆盖的线段就不能被看到 寻找被覆盖的线段利用区贪心间,我们按照左端点升序.左端点相同右端点降序排序,则左端点一定被前面的线段覆盖,接着对于右端点使用单调栈的思想寻找可以看到的线段就

简单几何(线段覆盖) POJ 3347 Kadj Squares

题目传送门 题意:告诉每个矩形的边长,它们是紧贴着的,问从上往下看,有几个还能看到. 分析:用网上猥琐的方法,将边长看成左端点到中心的距离,这样可以避免精度问题.然后先求出每个矩形的左右端点,然后如果被覆盖那么将端点更新到被覆盖的位置.最后看那些更新后左端点小于右端点,这些是可以看得到的. /************************************************ * Author :Running_Time * Created Time :2015/10/28 星期三

POJ 3347 Kadj Squares (线段覆盖)

题目大意:给你几个正方形的边长,正方一个顶点在x轴上然后边与x轴的夹角为45度,每个正方形都是紧贴的,问从上面看能看的正方形的编号 题目思路:线段覆盖,边长乘上2防止产生小数,求出每个正方形与x轴平行的对角线的起始x坐标,剩下的就是线段了. #include<cstdio> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<cs

POJ 3347 Kadj Squares

题意: 题目链接 思路: 最开始没思路不知道怎么判断 后来看了题解... 果然数据小(n<=50,len<=30)就可以随便瞎搞 开始时困扰我的是怎么求新加入的正方形的位置 原来是枚举已加入的每一个正方形,计算出紧挨当前正方形的位置,然后取max就可以了 至于正方形是否看得见 两两枚举判断(见代码) code:(这道题其实看代码超级好理解) #include<cstdio> #include<cmath> #include<algorithm> #inclu

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

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

题目链接: 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 quart

【POJ】2318 TOYS ——计算几何+二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

Poj 1556 The Doors 计算几何+最短路

其实本题非常的无脑,无脑拍完1A,写到blog里只因为TM无脑拍也拍了很久啊= = #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdl

POJ 3304 Segments(计算几何:直线与线段相交)

POJ 3304 Segments 大意:给你一些线段,找出一条直线能够穿过所有的线段,相交包括端点. 思路:遍历所有的端点,取两个点形成直线,判断直线是否与所有线段相交,如果存在这样的直线,输出Yes,但是注意去重. struct Point { double x, y; } P[210]; struct Line { Point a, b; } L[110]; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.