poj 1228 凸包
题目链接:http://poj.org/problem?id=1228
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std; const double eps = 1e-;
const double PI = acos(-1.0);
const double INF = 1000000000000000.000; struct Point{
double x,y;
Point(double x=, double y=) : x(x),y(y){ } //构造函数
};
typedef Point Vector; Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);}
Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);}
Vector operator * (double p,Vector A){return Vector(A.x*p,A.y*p);}
Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);} bool operator < (const Point& a,const Point& b){
return a.x < b.x ||( a.x == b.x && a.y < b.y);
} inline int dcmp(double x){
if(fabs(x) < eps) return ;
else return x < ? - : ;
}
bool operator == (const Point& a, const Point& b){
return dcmp(a.x - b.x) == && dcmp(a.y - b.y) == ;
} ///向量(x,y)的极角用atan2(y,x);
inline double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
inline double Length(Vector A) { return sqrt(Dot(A,A)); }
inline double Angle(Vector A, Vector B) { return acos(Dot(A,B) / Length(A) / Length(B)); }
inline double Cross(Vector A, Vector B) { return A.x*B.y - A.y * B.x; } //凸包:
/**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/ //Goal[]数组模拟栈的使用;
int ConvexHull(Point* P,int n,Point* Goal){
sort(P,P+n);
int m = unique(P,P+n) - P; //对点进行去重;
int cnt = ;
for(int i=;i<m;i++){ //求下凸包;
while(cnt> && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--; //如果希望在凸包的边上有输入点,把<= 换成 <.
Goal[cnt++] = P[i];
}
int temp = cnt;
for(int i=m-;i>=;i--){ //逆序求上凸包;
while(cnt>temp && dcmp(Cross(Goal[cnt-]-Goal[cnt-],P[i]-Goal[cnt-])) <= ) cnt--;
Goal[cnt++] = P[i];
}
if(cnt > ) cnt--; //减一为了去掉首尾重复的;
return cnt;
} bool IsPointOnSegment(Point P,Point A,Point B){
return dcmp(Cross(A-P,B-P)) == && dcmp(Dot(P-A,P-B)) < ;
} /*************************************分 割 线*****************************************/ const int maxn = ; Point P[maxn];
Point Goal[maxn]; int main()
{
freopen("E:\\acm\\input.txt","r",stdin); int T;
cin>>T; while(T--){
int n;
cin>>n; for(int i=;i<n;i++){
scanf("%lf %lf",&P[i].x,&P[i].y);
} int cnt = ConvexHull(P,n,Goal); if(cnt <= || cnt* > n || n < ){
printf("NO\n");
continue;
} cout<<cnt<<" "<<Goal[cnt].x<<" "<<Goal[cnt].y<<endl;
Goal[cnt] = Goal[];
int i;
for(i=;i<cnt;i++){
bool flag = false;
for(int j=;j<n;j++){
// if(j == i || j == i+1) continue; 加了这句就WA 7 次,痛苦啊
if(IsPointOnSegment(P[j],Goal[i],Goal[i+])){
flag = true;
}
}
if(!flag) break;
}
if(i<cnt) printf("NO\n");
else printf("YES\n");
}
}
poj 1228 凸包的更多相关文章
- POJ 1228 - Grandpa's Estate 稳定凸包
稳定凸包问题 要求每条边上至少有三个点,且对凸包上点数为1,2时要特判 巨坑无比,调了很长时间= = //POJ 1228 //稳定凸包问题,等价于每条边上至少有三个点,但对m = 1(点)和m = ...
- 凸包稳定性判断:每条边上是否至少有三点 POJ 1228
//凸包稳定性判断:每条边上是否至少有三点 // POJ 1228 #include <iostream> #include <cstdio> #include <cst ...
- poj 1228 稳定凸包
Grandpa's Estate Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12337 Accepted: 3451 ...
- POJ 1228 Grandpa's Estate 凸包 唯一性
LINK 题意:给出一个点集,问能否够构成一个稳定凸包,即加入新点后仍然不变. 思路:对凸包的唯一性判断,对任意边判断是否存在三点及三点以上共线,如果有边不满足条件则NO,注意使用水平序,这样一来共线 ...
- poj 1873 凸包+枚举
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6198 Accepted: 1 ...
- poj 1113 凸包周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33888 Accepted: 11544 Descriptio ...
- Poj 2187 凸包模板求解
Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...
- POJ 1228 Grandpa's Estate --深入理解凸包
题意: 判断凸包是否稳定. 解法: 稳定凸包每条边上至少有三个点. 这题就在于求凸包的细节了,求凸包有两种算法: 1.基于水平序的Andrew算法 2.基于极角序的Graham算法 两种算法都有一个类 ...
- 【POJ 1228】Grandpa's Estate 凸包
找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include ...
随机推荐
- C++ sizeof总结
关键字sizeof的作用是返回对象或类型占用的内存字节数,返回值是size_t. 基本数据类型占用的字节数:(32位系统) char 1字节 bool 1字节 short 2字节 int 4字节 lo ...
- java中 引用类型 和 基本类型 有何区别?
栈与堆都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. Java的堆是一个运行时数据区,类的(对象从中分配空间.这些对象通过new.newa ...
- html5+css3实现上拉和下拉刷新
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- 控制器view加载
出自李明杰讲课视频
- adb找不到设备
提示信息如下所示: adb server is out of date.killing... adb server didn't ACK *failed to start daemon * error ...
- 实用lsof常用命令行
1, 使用 lsof 命令行列出所有打开的文件 # lsof 这可是一个很长的列表,包括打开的文件和网络 上述屏幕截图中包含很多列,例如 PID.user.FD 和 TYPE 等等. FD - Fil ...
- CodeChef CBAL
题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...
- MongoDB update数据语法【转】
在前面的文章“mongodb 查询的语法”里,我介绍了Mongodb的常用查询语法,Mongodb的update操作也有点复杂,我结合自己的使用经验,在这里介绍一下,给用mongodb的朋友看看,也方 ...
- Apache与Nginx的比较
1.Apache与Nginx的优缺点比较 nginx相对于apache的优点: 轻量级 : 同样起web 服务,比apache 占用更少的内存及资源 抗并发 : nginx 处理请求是异步非阻塞的,而 ...
- [BZOJ 1016] [JSOI2008] 最小生成树计数 【DFS】
题目链接:BZOJ - 1016 题目分析 最小生成树的两个性质: 同一个图的最小生成树,满足: 1)同一种权值的边的个数相等 2)用Kruscal按照从小到大,处理完某一种权值的所有边后,图的连通性 ...