POJ3347 Kadj Squares(计算几何)

Kadj Squares

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3296 Accepted: 1307

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 xy 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 S1Si-1.

《POJ3347 Kadj Squares(计算几何)》

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

题意:给出n个正方形,斜四十五度摆放.求出没有被完全挡住的正方形.

思路:找到每个正方形最左边的位置和最右边的位置,转化成线段覆蓋问题.

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <set>
#include <map>
#include <queue>
using namespace std;
const int MAXN = 5000+10;
const int INF=1e9+7;
const double eps=1e-8;
const double pi=acos(-1.0);
//计算几何误差修正
//输入为一个double类型的数,返回-1表示负数,1表示正数,0表示x为0
int cmp(double x){
    if(fabs(x)<eps)
        return 0;
    if(x>0) return 1;
    return -1;
}

//计算几何点类
inline double sqr(double x){
    return x*x;
}

struct point{
    double x,y;
    int id;///
    point(){}
    point(double a,double b):x(a),y(b){}
    void input(){
        scanf("%lf%lf",&x,&y);
    }
    //加法
    friend point operator + (const point &a,const point &b){
        return point(a.x+b.x,a.y+b.y);
    }
    //减法
    friend point operator - (const point &a,const point &b){
        return point(a.x-b.x,a.y-b.y);
    }
    //判断相等
    friend bool operator == (const point &a,const point &b){
        return cmp(a.x-b.x)==0&&cmp(a.y-b.y)==0;
    }
    //倍增
    friend point operator * (const point &a,const double &b){
        return point(a.x*b,a.y*b);
    }
    //倍增
    friend point operator * (const double &b,const point &a){
        return point(a.x*b,a.y*b);
    }
    //除法
    friend point operator / (const point &a,const double b){
        return point(a.x/b,a.y/b);
    }
    //模长
    double norm(){
        return sqrt(sqr(x)+sqr(y));
    }
};

//叉积,a×b>0代表a在b的顺时针方向,<0代表a在b的逆时针方向,等于0代表a和b向量共线,但不确定方向是否相同
double det(const point &a,const point &b){
    return a.x*b.y-a.y*b.x;
}
//点积
double dot(const point &a,const point &b){
    return a.x*b.x+a.y*b.y;
}
//距离
double dist(const point &a,const point &b){
    return (a-b).norm();
}
//op向量绕原点逆时针旋转A(弧度)
point rotate_point(const point &p,double A){
    double tx=p.x,ty=p.y;
    return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
}

//计算几何线段类
struct line{
    point a,b;
    line(){}
    line(point x,point y):a(x),b(y){}
    void input(){
        a.input();
        b.input();
    }
};

//用两个点a,b生成的一个线段或者直线
line point_make_line(point a,point b){
    return line(a,b);
}

//求点p到线段st的距离
double dis_point_segment(point p,point s,point t){
    if(cmp(dot(p-s,t-s))<0) return (p-s).norm();
    if(cmp(dot(p-s,s-t))<0) return (p-t).norm();
    return fabs(det(s-p,t-p)/dist(s,t));
}
//求点p到线段st的垂足,保存在cp中
void PointProjLine(point p,point s,point t,point &cp){
    double r=dot((t-s),(p-s))/dot(t-s,t-s);
    cp=s+(t-s)*r;
}

//判断p点是否在线段st上
bool PointOnSegment(point p,point s,point t){
    return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;
}

//判断a和b是否平行
bool parallel(line a,line b){
    return !cmp(det(a.a-a.b,b.a-b.b));
}

//判断a和b是否共线
bool contribution(line a,line b){
    if(!parallel(a, b))
        return false;
    if(!cmp(det(a.b-a.a,b.a-a.b)))
        return true;
    return false;
}

//判断a和b是否相交,若相交则返回true且交点保存在res中
bool line_make_point(line a,line b,point &res){
    if(parallel(a, b))
        return false;
    double s1=det(a.a-b.a,b.b-b.a);
    double s2=det(a.b-b.a,b.b-b.a);
    res=(s1*a.b-s2*a.a)/(s1-s2);
    return true;
}
//判断线段是否相交
bool segment_make_point(line a,line b,point &res){
    if(PointOnSegment(a.a, b.a, b.b)||PointOnSegment(a.b, b.a, b.b)||PointOnSegment(b.a, a.a, a.b)||PointOnSegment(b.b, a.a, a.b))
        return true;
    if(!line_make_point(a,b,res))
        return false;
    if(PointOnSegment(res, a.a, a.b)&&PointOnSegment(res, b.a, b.b))
        return true;
    return false;
}
//判断线段和直线是否相交,a是直线,b是线段
bool line_across_segment(line a,line b,point &res){
    if(cmp(det(a.b-a.a,b.a-a.a)*det(a.b-a.a,b.b-a.a))==1){
        return false;
    }
    line_make_point(a, b, res);
    return true;
}

//将直线a沿法向量方向平移距离len得到的直线
line move_d(line a,const double &len){
    point d=a.b-a.a;
    d=d/d.norm();
    d=rotate_point(d, pi/2);
    return line(a.a+d*len,a.b+d*len);
}
double a[MAXN];
double b[MAXN];
double l[MAXN],r[MAXN];
int main(){
    int n;
    while(scanf("%d",&n)&&n){
        for(int i=0;i<n;i++){
            scanf("%lf",a+i);
            double temp=0;
            b[i]=a[i];
            if(i){
                for(int j=0;j<i;j++){
                    temp=b[j]+min(a[i],a[j])*2;
                    b[i]=max(b[i],temp);
                }
            }
            l[i]=b[i]-a[i];
            r[i]=b[i]+a[i];
        }
        for(int i=1;i<n;i++){
            for(int j=0;j<i;j++){
                if(cmp(a[j]-a[i])==0)
                    continue;
                if(a[j]<a[i]&&r[j]>l[i]){
                    r[j]=l[i];
                }else if(a[i]<a[j]&&r[j]>l[i]){
                    l[i]=r[j];
                }
            }
        }
        for(int i=0;i<n;i++){
            if(l[i]<r[i]){
                printf("%d ",i+1);
            }
        }
        printf("\n");
    }
}

点赞