博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2653 Pick-up sticks(计算几何)
阅读量:4670 次
发布时间:2019-06-09

本文共 3274 字,大约阅读时间需要 10 分钟。

Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 7946 Accepted: 2916

Description

Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.

Input

Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.

Output

For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown. 
The picture to the right below illustrates the first case from input.
POJ 2653 Pick-up sticks(计算几何) - qhn999 - 码代码的猿猿

Sample Input

5
1 1 4 2
2 3 3 1
1 -2.0 8 4
1 4 8 2
3 3 6 -2.0
3
0 0 1 1
1 0 2 1
2 0 3 1
0

Sample Output

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

Hint

Huge input,scanf is recommended.

Source

暴力的时候需要由前面的往后判断,这样就不会达到nlogn了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const double eps=1e-8;
int cmp(double x)
{
    if(fabs(x)<eps) return 0;
    if(x>0) return 1;
    return -1;
}
struct point
{
    double x,y;
    point(double a,double b):x(a),y(b) {}
    point() {}
};
struct line
{
    point a,b;
    line(point x,point y):a(x),b(y)  {}
    line() {}
};
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;
}
point operator-(const point&a,const point&b)
{
    return point(a.x-b.x,a.y-b.y);
}
point operator*(const point&a,const double &b)
{
    return point(a.x*b,a.y*b);
}
point operator*(const double &a,const point &b)
{
    return point(a*b.x,a*b.y);
}
point operator/(const point&a,const double &b)
{
    return point(a.x/b,a.y/b);
}
bool parallel(line a,line b)
{
    return !cmp(det(a.a-a.b,b.a-b.b));
}
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 pointonsegment(point p,point s,point t)
{
    return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;
}
line LL[100100];int vis[100100];
int main()
{
    int n;
while(scanf("%d",&n)!=EOF&&n)
{
    memset(vis,-1,sizeof(vis));
    for(int i=0;i<n;i++)
    {
        double a,b,c,d;
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        point A(a,b),B(c,d);
        LL
.a=A; LL.b=B;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            point res;
            res.x=999999;
            res.y=-99999;
            if(vis
==0) break;
            if(line_make_point(LL
,LL[j],res)&&pointonsegment(res,LL[j].a,LL[j].b)&&pointonsegment(res,LL.b,LL.a))
            {
                vis
=0;
            }
        }
    }
    int first=1;
    printf("Top sticks: ");
    for(int i=0;i<n;i++)
    {
        if(vis
==-1)
        {
            if(first==1)
            {
                first=0;
            }
            else
            {
                printf(", ");
            }
            printf("%d",i+1);
        }
    }
    printf(".\n");
}
    return 0;
}

转载于:https://www.cnblogs.com/CKboss/p/3350947.html

你可能感兴趣的文章
sed系列:行或者模式匹配删除特定行
查看>>
python常见面试题(三)
查看>>
回文日期(NOIP2016 普及组第二题)
查看>>
[jQuery]回到顶部
查看>>
Win7下修改Hosts文件
查看>>
Linq to sql并发与事务
查看>>
2017-06-27
查看>>
Convert DataTable to Html Table
查看>>
JavaEE复习三
查看>>
全局ajax事件
查看>>
javascript二维数组
查看>>
JavaScript 字符串属性和方法
查看>>
opencv新手注意
查看>>
Source InSight context 窗口丢失的解决办法
查看>>
cut point and bridge总结
查看>>
(5)Oracle基础--约束
查看>>
【Nginx】磁盘文件写入飞地发
查看>>
默认情况下安装的应用程序C盘后提示权限不足,当你开始介意。。。
查看>>
su root 后还是不能使用useradd ,useradd 等命令
查看>>
URL.createObjectURL图片预览
查看>>