标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1098 Accepted Submission(s): 598
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
#define MM(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
using namespace std;
struct Point{
int x,y;
void read()
{
scanf("%d%d",&x,&y);
}
}p[105],tubao[105];
int dcmp(double a)
{
if(fabs(a)<eps) return 0;
else if(a>0) return 1;
else return -1;
}
Point operator-(Point a,Point b)
{
return (Point){a.x-b.x,a.y-b.y};
}
double dis(Point a)
{
return sqrt(a.x*a.x+a.y*a.y);
}
double cross(Point a,Point b)
{
return a.x*b.y-b.x*a.y;
}
double dot(Point a,Point b)
{
return a.x*b.x+a.y*b.y;
}
bool cmp(Point a,Point b)
{
if(a.x!=b.x) return a.x<b.x;
else return a.y<b.y;
}
int convex_hull(Point *p,int n,Point *tubao)
{
sort(p+1,p+n+1,cmp);
int m=0;
for(int i=1;i<=n;i++)
{
while(m>=2&&cross(p[i]-tubao[m-1],tubao[m]-tubao[m-1])>0) m--;
tubao[++m]=p[i];
}
int k=m;
for(int i=n-1;i>=1;i--)
{
while(m-k>=1&&cross(p[i]-tubao[m-1],tubao[m]-tubao[m-1])>0) m--;
tubao[++m]=p[i];
}
m--;
return m;
}
int main()
{
int cas,n;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) p[i].read();
int k=convex_hull(p,n,tubao);
tubao[k+1]=tubao[1];
bool flag=true;
double tmp=(n-2.0)*pi/n;
for(int i=1;i<=k-1;i++)
{
Point a=tubao[i+1]-tubao[i],b=tubao[i+2]-tubao[i+1];
double cosang=dot(a,b)/(dis(a)*dis(b));
double ang=acos(cosang);
ang=pi-ang;
if(dcmp(ang-tmp)!=0) {flag=false;break;}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
分析:主要是借助这道题来分下下计算几何的精度问题,
double型数据精度处理的两种方式
1.相除改为ong long相乘,这种是肯定对的,不会错。
2.dcmp函数,这种比较简单,但是有一定的精度条件,如果角度是1/999999-1/1000000,那么相减起来就是1e-6*1/999999为1e-12级别,这样是可以使用dcmp的,比如本道题,因为1-e4<=x<=1e4,那么最小的角度差是1/(2*1e4-1)-1/2*1e4(最小的角是1/2*1e4,第二小的角度是1/(2*1e4-1))为1e-8级别>1e-12级别,所以可以用dcmp(eps<1e-12)
标签:
原文地址:http://www.cnblogs.com/smilesundream/p/5768914.html