POJ 3347(计算几何)

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

(新人写博客,有错请及时指出,谢谢)
这个题我的思路很蠢
就是先求出每个正方形下顶点的横坐标再O(n^2)比较,看看每个正方形的左边和右边分别被遮住了多少,如果被全部遮住那么这个正方形就不可见
后面判断遮住没有倒好说,对我个人而言麻烦的是求每个正方形的顶点横坐标(快被这个逼疯了)
然后就找到一个很巧妙的方法
对于每个正方形确定了左顶点就确定了其他顶点,所以我们可以先找左顶点
对于每一个正方形,我们可以假设让它与每一个在它左边的正方形贴紧一次(以题目要求的方式),并求出每一次贴紧时得到的该正方形的左顶点的横坐标,这些横坐标中最大的那个就是它的横坐标
比如图中让S3分别和S2,S1贴紧(与S1贴紧时忽略S2的存在)分别得到两个横坐标X1(与S1贴紧),X2(与S2贴紧),由于X2>X1,所以S3的左顶点横坐标为S2


代码:
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<utility>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<math.h>
#include<iterator>
#include<stack>
using namespace std;
typedef long long LL;
const double eps=1e-8,PI=3.1415926538;
const LL MOD=1000000000+7;
double side[111],bi[111];
int n;


int sign(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0) return -1;
    return 1;
}
/*int gcd(int a,int b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}*/
/*int abs(int a)
{
    return a>0?a:-a;
}*/
LL fast(LL a,LL b)
{
    LL sum=1;
    while(b>0)
    {
        if(b&1)sum=(sum*a%MOD+MOD)%MOD;
        a=(a*a%MOD+MOD)%MOD;
        b>>=1;
    }
    return (sum%MOD+MOD)%MOD;
}


struct P
{
    int pos;
    double angle;
    double x,y;
    P(){}
    P(double x1,double y1):x(x1),y(y1){angle=atan2(y,x);}
    P operator +(P p){return P(x+p.x,y+p.y);}
    P operator -(P p){return P(x-p.x,y-p.y);}
    P operator *(double d){return P(x*d,y*d);}
    P operator /(double d){return P(x/d,y/d);}
    bool operator !=(P p)
    {
        if(fabs(x-p.x)<eps&&fabs(y-p.y)<eps)return false;
        return true;
    }
    bool operator ==(P p)
    {
        return !((*this)!=p);
    }
    double dot(P p){return (x*p.x+y*p.y);}//点乘
    double det(P p){return (x*p.y-y*p.x);}//叉乘
};
//判断点q是否在线段p1-p2上
bool onseg(P p1,P p2,P q)
{
    return fabs((p1-q).det(p2-q))<=eps&&(p1-q).dot(p2-q)<=eps;
}


//计算直线p1-p2与q1-q2的交点
P intersection(P p1,P p2,P q1,P q2)
{
    return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}

//判断两直线是否平行true:平行
bool parallel(P L1,P L2)
{
    return fabs(L1.det(L2))<=eps;
}
//判断是否严格相交
bool insex(P p1,P p2,P q1,P q2)
{
    return (((q1-p1).det(q2-p1)*(q1-p2).det(q2-p2)<0)&&((p1-q1).det(p2-q1)*(p1-q2).det(p2-q2)<0));
}






int quad(P a)// 判断象限的函数,每个象限包括半个坐标轴
{
    if( a.x>=0&&a.y>=0 ) return 1;
    if( a.x<=0&&a.y>=0 ) return 2;
    if( a.x<=0&&a.y<=0 ) return 3;
    if( a.x>=0&&a.y<=0 ) return 4;
}

/*******************************************/
/*******************************************/
/*******************************************/

int order[111];
int main()
{
    while(scanf("%d",&n)!=-1&&n!=0)
    {
        side[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf",&side[i]);
        }
        side[n+1]=0;
        bi[0]=0;
        bi[1]=side[1]/sqrt(2.0);
        for(int i=2;i<=n;i++)
        {
            double bottom=0;
            for(int j=1;j<i;j++)
            {
                double co,si=side[j];
                if(side[i]<side[j])si=side[i];
                co=bi[j]+sqrt(2.0)*si;
                bottom=max(bottom,co);
            }
            bi[i]=bottom;
        }//求下顶点
        /*for(int i=1;i<=n;i++)
        {
            printf("%.2lf\n",bi[i]);
        }*/
        int t=0;
        for(int i=1;i<=n;i++)
        {
            double now=side[i]*sqrt(2.0);
            double left=0,right=0;
            for(int j=1;j<=n;j++)
            {
                if(i==j)continue;
                else if(i>j)
                {
                   double dis=bi[i]-bi[j];
                   dis=(now/2+side[j]/sqrt(2.0))-dis;
                   if(dis>0&&side[j]>side[i])left=max(left,dis);
                }
                else
                {
                    double dis=bi[j]-bi[i];
                    dis=(now/2+side[j]/sqrt(2.0))-dis;
                    if(dis>0&&side[j]>side[i])right=max(right,dis);
                }
            }
            //printf("i=%d left=%.2f right=%.2f\n",i,left,right);
            if(left+right>=now||fabs(left+right-now)<eps)continue;//判断遮住没
            else
            {
                order[t++]=i;
            }
        }
        for(int i=0;i<t;i++)
        {
            printf("%d",order[i]);
            if(i<t-1)putchar(' ');
        }
        putchar('\n');
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值