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");
}
}
上一篇:转:有关Java泛型的类型擦除(type erasing)


下一篇:NLP系列(2)_用朴素贝叶斯进行文本分类(上)