UVALive
来源:互联网 发布:网络教育比函授贵吗 编辑:程序博客网 时间:2024/05/24 06:57
题目链接点这里
对于判断点是否在凸多边形内有logn的算法,,可以看这一篇博客
HLG 1429 凸多边形【快速判断点在凸多边形内】
#include<iostream>#include<cstdio>#include<math.h>#include<algorithm>#include<map>#include<set>#include<bitset>#include<stack>#include<queue>#include<string.h>#include<cstring>#include<vector>#include<time.h>#include<stdlib.h>using namespace std;#define INF 0x3f3f3f3f#define INFLL 0x3f3f3f3f3f3f3f3f#define FIN freopen("input.txt","r",stdin);#define sf(x,y) scanf("x",y)#define pf(x,y) printf("x",y)#define mem(x,y) memset(x,y,sizeof(x))typedef unsigned long long ULL;typedef long long LL;#define fuck(x) cout<<"["<<x<<"]"<<endl;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef pair<pair<LL,LL>,LL> PIII;typedef pair<LL,LL> PII;const int MX=1e4+10;double eps=1e-8;int L,S;struct Point { double x, y; Point() {} Point(double x,double y):x(x),y(y) {} bool operator<(const Point&b) const { if(x != b.x) return x < b.x; return y < b.y; } Point operator-(const Point &P) const { return Point(x - P.x, y - P.y);; } double operator^(const Point &P) const { //叉积 return x * P.y - y * P.x;//如果改成整形记得加LL } double operator*(const Point &P) const { //点积 return x * P.x + y * P.y;//如果改成整形记得加LL }};typedef Point Vector;int sign(double x) { if(fabs(x)<eps)return 0; return x<0?-1:1;}int dcmp(double a,double b) { if(fabs(a-b)<eps)return 0; else if(a>b)return 1; else return -1;}Point P[MX], R[MX];int convex(int n) { int m = 0, k; sort(P, P + n); for(int i = 0; i < n; i++) { while(m > 1 && sign((R[m - 1]-R[m-2])^(P[i]-R[m - 2]))<= 0) m--; R[m++] = P[i]; } k = m; for(int i = n - 2; i >= 0; i--) { while(m > k && sign((R[m - 1]-R[m-2])^(P[i]-R[m - 2]))<= 0) m--; R[m++] = P[i]; } if(n > 1) m--; return m;}//nlogn计算x是否在凸多边形内int is_in(Point P,Point *R,int n) { //-1不在,0在边上,1在内部 int l=1, r=n-2,mid; while(l<=r) { mid = (l+r) >> 1; int t1 = sign((R[mid]-R[0])^(P-R[0])); int t2 = sign((R[mid+1]-R[0])^(P-R[0])); if(t1>=0 &&t2<=0) { int t3=sign((P-R[mid])^(P-R[mid+1])); if(t3 < 0) return -1; else if(t1||t2||t3)return 0; return 1; } else if(t1 < 0)r = mid -1; else l = mid+1; } return -1;}int main() { FIN; while(~scanf("%d",&L)) { for(int i=0; i<L; i++)scanf("%lf%lf",&P[i].x,&P[i].y); L=convex(L); scanf("%d",&S); int ans=0; for(int i=0; i<S; i++) { double x,y; scanf("%lf%lf",&x,&y); if(is_in(Point(x,y),R,L)>=0)ans++; } cout<<ans<<endl; } return 0;}
阅读全文
0 0
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- UVALive
- 12000字揭秘阿里连环炮面试
- hdu6052To my boyfriend(子矩阵计数处理)
- 2017 Multi-University Training Contest
- 倒角距离匹配
- google GMS认证和搭建CTS 环境一
- UVALive
- 二级制代码扫描工具
- 二维数组按照其内层数组的某个键值排序
- 在Ubuntu中添加用户
- Android O 新特性和行为变更总结
- 快速了解一个行业的秘籍,你值得拥有
- 大数相加问题(java版 )
- Android 解决输入法遮挡布局控件的问题
- 小光棍数