POJ 1912 A highway and the seven dwarfs (凸包)
【题目链接】 http://poj.org/problem?id=1912
【题目大意】
给出一些点,表示一些屋子,这些屋子共同组成了村庄,现在要建一些高速公路
问是否经过了村庄。
【题解】
这些屋子的关键点一定在凸包上,所以我们只要求出凸包,判断是否和线相交即可
我们求出与高速公路相近和近似相反的向量,判断连线是否与这条公路相交即可。
【代码】
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
double EPS=1e-10;
const double PI=acos(-1.0);
double add(double a,double b){
if(abs(a+b)<EPS*(abs(a)+abs(b)))return 0;
return a+b;
}
struct P{
double x,y;
P(){}
P(double x,double y):x(x),y(y){}
P operator + (P p){return P(add(x,p.x),add(y,p.y));}
P operator - (P p){return P(add(x,-p.x),add(y,-p.y));}
P operator * (double d){return P(x*d,y*d);}
double dot(P p){return add(x*p.x,y*p.y);} //点积
double det(P p){return add(x*p.y,-y*p.x);} //叉积
};
bool cmp_x(const P& p,const P& q){
if(p.x!=q.x)return p.x<q.x;
return p.y<q.y;
}
vector<P> convex_hull(P* ps,int n){
sort(ps,ps+n,cmp_x);
int k=0;
vector<P> qs(n*2);
for(int i=0;i<n;i++){
while((k>1)&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0)k--;
qs[k++]=ps[i];
}
for(int i=n-2,t=k;i>=0;i--){
while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0)k--;
qs[k++]=ps[i];
}qs.resize(k-1);
return qs;
}
double dist(P p,P q){return sqrt((p-q).dot(p-q));}
double normalize(double r){
if(r<-PI/2.0+EPS)r+=PI*2;
return r;
}
double atan2(const P& p){
return normalize(atan2(p.y, p.x));
}
bool double_cmp(double a,double b){
return a+EPS<b;
}
const int MAX_N=100010;
int N,n;
P ps[MAX_N];
double as[MAX_N];
void solve(){
for(int i=0;i<N;i++)scanf("%lf%lf",&ps[i].x,&ps[i].y);
vector<P> chs;
if(N>1){
chs=convex_hull(ps,N);
n=chs.size();
chs.push_back(chs[0]);
}
for(int i=0;i<n;i++)as[i]=atan2(chs[i+1]-chs[i]);
sort(as,as+n,double_cmp);
P p1,p2;
while(~scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y)){
if(N<2){puts("GOOD");continue;}
int x=upper_bound(as,as+n,atan2(p2-p1),double_cmp)-as;
int y=upper_bound(as,as+n,atan2(p1-p2),double_cmp)-as;
puts((((p2-p1).det(chs[x]-p1)*(p2-p1).det(chs[y]-p1)>-EPS))?"GOOD":"BAD");
}
}
int main(){
while(~scanf("%d",&N))solve();
return 0;
}
POJ 1912 A highway and the seven dwarfs (凸包)的更多相关文章
- poj 1912 A highway and the seven dwarfs
A highway and the seven dwarfs Time Limit: 8000MS Memory Limit: 30000K Total Submissions: 2622 A ...
- POJ1912 A highway and the seven dwarfs (判断凸包与直线相交 logn)
POJ1912 给定n个点 和若干条直线,判断对于一条直线,是否存在两个点在直线的两侧. 显然原命题等价于 凸包与直线是否相交. O(n)的算法是显而易见的 但是直线数量太多 就会复杂到O(n^2)由 ...
- POJ 1912 凸包
题目: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib& ...
- POJ 2187 Beauty Contest【旋转卡壳求凸包直径】
链接: http://poj.org/problem?id=2187 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- POJ 2225 / ZOJ 1438 / UVA 1438 Asteroids --三维凸包,求多面体重心
题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...
- poj 2187:Beauty Contest(计算几何,求凸包,最远点对)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 26180 Accepted: 8081 D ...
- POJ 3135 Polygons on the Grid(枚举+凸包)
题目大意是让你用这n条边放在网格上构成凸包,并且边的两端点必须在网格上. 那么比较容易想到的就是枚举可能情况,因为这样的勾股数组成情况不多,因此可以直接枚举所有连出去的边反映在坐标轴上的所有情况,最后 ...
- 【POJ】2187 Beauty Contest(旋转卡壳)
http://poj.org/problem?id=2187 显然直径在凸包上(黑书上有证明).(然后这题让我发现我之前好几次凸包的排序都错了QAQ只排序了x轴.....没有排序y轴.. 然后本题数据 ...
- POJ 3384 Feng Shui
http://poj.org/problem?id=3384 题意:给一个凸包,求往里面放两个圆(可重叠)的最大面积时的两个圆心坐标. 思路:先把凸包边往内推R,做半平面交,然后做旋转卡壳,此时得到最 ...
随机推荐
- 在Global.asax中过滤POST请求的非法参数。
using System;using System.Collections.Generic;using System.Collections.Specialized;using System.Linq ...
- java md5加密 不依赖base64包
/** * MD5 加密 */ private String getMD5Str(String str) { MessageDigest messageDigest = null; try { mes ...
- HDU2546饭卡---(DP 经典背包)
http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/Others) Memory L ...
- 滑杆(JSlider)和进度指示条(JProgressBar) 的使用
package first; import javax.swing.*; import javax.swing.border.TitledBorder; import java.awt.*; impo ...
- C# 文件类的操作---获取
如何获取指定目录包含的文件和子目录 . DirectoryInfo.GetFiles():获取目录中(不包含子目录)的文件,返回类型为FileInfo[],支持通配符查找: . DirectoryIn ...
- adt 运行时,显示no target selected.
检查adt\adt-bundle-windows-x86-20131030\sdk\system-images下面是否有相关image文件.
- UVA 10183 How Many Fibs?
高精度推出大概600项fabi数,就包含了题目的数据范围,对于每组a,b,从1到600枚举res[i]即可 可以直接JAVA大数.我自己时套了C++高精度的版 JAVA 复制别人的 import ja ...
- IPython Notebook error: Error loading notebook
打开jupyter突然报错: An unknown error occurred while loading this notebook. This version can load notebook ...
- 【洛谷P3709】大爷的字符串题
看这题网上居然还没人写blog,怕是都去看洛谷自带的了-- 你才是字符串!你全家都是字符串!这题跟字符串没多大关系,只是出题人lxl想要吐槽某中学而已--... 其实这题说白了就是问区间里出现最多的数 ...
- jQuery点击自身以外地方关闭弹出层
转载自:http://blog.163.com/abkiss@126/blog/static/325941002012111754349590/ 主要功能是点击显示,然后通过点击页面的任意位置都能关闭 ...