Segments

Time Limit: 1000MS Memory Limit: 65536K

Description

Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on it, all projected segments have at least one point in common.

Input

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing a positive integer n ≤ 100 showing the number of segments. After that, n lines containing four real numbers x1 y1 x2 y2 follow, in which (x1, y1) and (x2, y2) are the coordinates of the two endpoints for one of the segments.

Output

For each test case, your program must output “Yes!”, if a line with desired property exists and must output “No!” otherwise. You must assume that two floating point numbers a and b are equal if |a - b| < 10-8.

Sample Input

3

2

1.0 2.0 3.0 4.0

4.0 5.0 6.0 7.0

3

0.0 0.0 0.0 1.0

0.0 1.0 0.0 2.0

1.0 1.0 2.0 1.0

3

0.0 0.0 0.0 1.0

0.0 2.0 0.0 3.0

1.0 1.0 2.0 1.0

Sample Output

Yes!

Yes!

No!

Source

Amirkabir University of Technology Local Contest 2006

又是一道基础的计算几何题,就是询问是否存在一条直线穿过给定的所有线段,由于n" role="presentation" style="position: relative;">nn很小,我们直接暴力枚举两个端点表示直线然后再O(n)" role="presentation" style="position: relative;">O(n)O(n)判断就行了(本蒟蒻因为有个return" role="presentation" style="position: relative;">returnreturn没有写调了40min" role="presentation" style="position: relative;">40min40min)。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define eps 1e-8
#define N 105
using namespace std;
struct pot{double x,y;}p[N<<1];
int n,t;
inline int sign(double x){return (x>eps)-(x<-eps);}
inline pot operator-(pot a,pot b){return pot{a.x-b.x,a.y-b.y};}
inline double cross(pot a,pot b){return a.x*b.y-a.y*b.x;}
inline bool ok(pot a,pot b,pot c,pot d){
    if((cross(a-c,b-c)*cross(a-d,b-d))<=0.0000)return true;
    return false;
}
inline bool pd(pot a,pot b){
    for(int i=1;i<n;i+=2)if(ok(a,b,p[i],p[i+1])==0)return false;
    return true;
}
inline bool check(){
    for(int i=1;i<n;++i)
        for(int j=i+1;j<=n;++j){
            if(sign(p[i].x-p[j].x)==0&&sign(p[i].y-p[j].y)==0)continue;
            if(pd(p[i],p[j]))return true;
        }
    return false;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        n<<=1;
        for(int i=1;i<=n;++i)scanf("%lf%lf",&p[i].x,&p[i].y);
        if(check())puts("Yes!");
        else puts("No!");
    }
    return 0;
}

2018.07.04 POJ 3304 Segments(简单计算几何)的更多相关文章

  1. 2018.07.04 POJ 1265 Area(计算几何)

    Area Time Limit: 1000MS Memory Limit: 10000K Description Being well known for its highly innovative ...

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

    POJ 3304 Segments 大意:给你一些线段,找出一条直线可以穿过全部的线段,相交包含端点. 思路:遍历全部的端点,取两个点形成直线,推断直线是否与全部线段相交,假设存在这种直线,输出Yes ...

  3. 2018.07.04 POJ 2398 Toy Storage(二分+简单计算几何)

    Toy Storage Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad have a problem: their ch ...

  4. 2018.07.04 POJ 1654 Area(简单计算几何)

    Area Time Limit: 1000MS Memory Limit: 10000K Description You are going to compute the area of a spec ...

  5. 2018.07.04 POJ 1113 Wall(凸包)

    Wall Time Limit: 1000MS Memory Limit: 10000K Description Once upon a time there was a greedy King wh ...

  6. 2018.07.04 POJ 1696 Space Ant(凸包卷包裹)

    Space Ant Time Limit: 1000MS Memory Limit: 10000K Description The most exciting space discovery occu ...

  7. POJ 3304 Segments 判断直线和线段相交

    POJ 3304  Segments 题意:给定n(n<=100)条线段,问你是否存在这样的一条直线,使得所有线段投影下去后,至少都有一个交点. 思路:对于投影在所求直线上面的相交阴影,我们可以 ...

  8. POJ 3304 Segments(判断直线与线段是否相交)

    题目传送门:POJ 3304 Segments Description Given n segments in the two dimensional space, write a program, ...

  9. POJ 3304 Segments (判断直线与线段相交)

    题目链接:POJ 3304 Problem Description Given n segments in the two dimensional space, write a program, wh ...

随机推荐

  1. 使用django + celery + redis 异步发送邮件

    参考:http://blog.csdn.net/Ricky110/article/details/77205291 环境: centos7  +  python3.6.1 + django2.0.1  ...

  2. Qt 软件的发布

    我们程序的Release版本正式发布需要将各种依赖的库文件一起打包. 有时候我们并不清楚具体依赖哪些库,这时,可以用Qt的一个工具"windeployqt" 比如,找到程序.exe ...

  3. iphone splash screen

    https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articl ...

  4. visual stdio 工程 宏

    $(SolutionDir)  solution目录 $(ProjectDir) Project目录 $(TargetDir) 目标文件夹,如编译出的exe文件所在的目录 $(Configuratio ...

  5. arguments.callee 属性 递归调用 & caller和callee的区别

    arguments.callee   在函数内部,有两个特殊的对象:arguments 和 this.其中, arguments 的主要用途是保存函数参数, 但这个对象还有一个名叫 callee 的属 ...

  6. webserive学习记录2-cxf框架基础使用

    cxf是一个webservice的框架,类似的还有axis,下面说一下cxf的基本使用. 首先要下载cxf的文件,然后要在项目中引入jar包,当然也可以通过maven进行管理.我用的是最新的3.2.1 ...

  7. Javascript系列:总体理解

    js是一个脚本客户端(浏览器)语言.和sql html类似.多练习. 没有排错的经验,弱类型语言,浏览器解释执行,出错也不会报错 预备 <!DOCTYPE html PUBLIC "- ...

  8. Hibernate的一个问题object references an unsaved transient instance - save the transi5

    1 我做了一对多和多对一的双向关联关系,在User这一方@ManyToOne已经设置了级联Cascade,这样在测试程序中保存User时,Group也应该自动保存,为什么会抛出以下的异常: (我是按着 ...

  9. shell 一次移动很多个命名相似的文件

    文件夹下面有很多类似下面命名的文件 aaaaaa01bbb aaaaaa01cc aaaaaa01dd aaaaaa02bbb aaaaaa02cc 要把 aaaaaa01 的文件移走 用 mv  / ...

  10. Java中Generics的使用

    1.Java的Generics与C++的Template由于Java的Generics设计在C++的Template之后,因此Java的Generics设计吸取Template的很多经验和教训.首先, ...