hdu 3629 Convex
2015-08-11 16:35
369 查看
题意:给你N个点,让你选四个点组成凸多边形,求总的方法数
详细解释:http://blog.sina.com.cn/s/blog_64675f540100ksug.html
详细解释:http://blog.sina.com.cn/s/blog_64675f540100ksug.html
#include<iostream> #include<vector> #include<cstring> #include<cstdio> #include<cmath> #include<stdlib.h> #include<queue> #include<map> #include<algorithm> using namespace std; const double eps = 1e-12; const double pi = acos(-1.0); const double INF = 10001000; double sqr(double x){ return x*x; } int cmp(double x){ if(fabs(x)<eps) return 0; if(x>0) return 1; return -1; } struct point{ double x,y; int index; point(){} point(double a,double b):x(a),y(b){} void input(){ scanf("%lf%lf",&x,&y); } double angle() { return atan2(y, x); } friend point operator + (const point &a,const point &b){ return point(a.x+b.x,a.y+b.y); } friend point operator - (const point &a,const point &b){ return point(a.x-b.x,a.y-b.y); } friend bool operator == (const point &a,const point &b){ return (cmp(a.x-b.x)==0)&&(cmp(a.y-b.y))==0; } friend point operator * (const point &a,double b){ return point(a.x*b,a.y*b); } friend point operator / (const point &a,double b){ return point(a.x/b,a.y/b); } friend double operator ^ (const point &a,const point &b) { return a.x*b.y - a.y*b.x; } double operator *(const point &b)const { return x*b.x + y*b.y; } double norm(){ //向量的模长 return sqrt(sqr(x)+sqr(y)); } }; double det(const point &a,const point &b){ //向量的叉集 return a.x*b.y-a.y*b.x; } double dot(const point &a,const point &b){ //向量的点集 return a.x*b.x+a.y*b.y; } double dot(const point &a,const point &b,const point &c){ //向量的点集ba 到bc return dot(a-b,c-b); } double dist(const point &a,const point &b){ //两点间的距离 return (a-b).norm(); } point rotate_point(const point &p,double A){ // 绕原点逆时针旋转 A(弧度) double tx=p.x,ty=p.y; return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A)); } //向量的旋转 //底边线段ab 绕a逆时针旋转角度A,b->b1,sinl是sinA的值。 point rotate_point(double cosl,double sinl,point a, point b){ b.x -= a.x; b.y -= a.y; point c; c.x = b.x * cosl - b.y * sinl + a.x; c.y = b.x * sinl + b.y * cosl + a.y; return c; } double xml(point x,point t1,point t2){ // 如果值为正,(t1-x)在(t2-x)的瞬时间方向 return det((t1-x),(t2-x)); } double area(point x,point y,point z){ return (det(y-x,z-x)); } struct line { point a,b; line(){} line(point x,point y):a(x),b(y){} }; point P_chuizhi_line(point a,point l1,point l2) // 求一个点,使得ac垂直于l1l2 { point c; l2.x -= l1.x; l2.y -= l1.y; c.x = a.x - l1.x - l2.y + l1.x; c.y = a.y - l1.y + l2.x + l1.y; return c; } point P_To_seg(point P,line L) //点到线段 最近的一个点 { point result; double t = ((P-L.a)*(L.b-L.a))/((L.b-L.a)*(L.b-L.a)); if(t >= 0 && t <= 1) { result.x = L.a.x + (L.b.x - L.a.x)*t; result.y = L.a.y + (L.b.y - L.a.y)*t; } else { if(dist(P,L.a) < dist(P,L.b)) result = L.a; else result = L.b; } return result; } double dis_p_to_line(point p,line l){ //点到直线的距离 return fabs(area(p,l.a,l.b))/dist(l.a,l.b); } double dis_p_to_seg(point p,line l) //点到线段的距离 { return dist(p,P_To_seg(p,l)); } double dis_pall_seg(point p1, point p2, point p3, point p4) //平行线段之间的最短距离 { return min(min(dis_p_to_seg(p1,line(p3,p4)), dis_p_to_seg(p2, line(p3, p4))), min(dis_p_to_seg(p3,line(p1, p2)), dis_p_to_seg(p4,line(p1, p2))) ); } bool intbr(line l1,line l2) { // 线段相交 return max(l1.a.x,l1.b.x) >= min(l2.a.x,l2.b.x) && max(l2.a.x,l2.b.x) >= min(l1.a.x,l1.b.x) && max(l1.a.y,l1.b.y) >= min(l2.a.y,l2.b.y) && max(l2.a.y,l2.b.y) >= min(l1.a.y,l1.b.y) && cmp((l2.a-l1.a)^(l1.b-l1.a))*cmp((l2.b-l1.a)^(l1.b-l1.a)) <= 0 && cmp((l1.a-l2.a)^(l2.b-l2.a))*cmp((l1.b-l2.a)^(l2.b-l2.a)) <= 0; } point line_inter(point A,point B,point C,point D){ //直线相交交点 point ans; double a1=A.y-B.y; double b1=B.x-A.x; double c1=A.x*B.y-B.x*A.y; double a2=C.y-D.y; double b2=D.x-C.x; double c2=C.x*D.y-D.x*C.y; ans.x=(b1*c2-b2*c1)/(a1*b2-a2*b1); ans.y=(a2*c1-a1*c2)/(a1*b2-a2*b1); return ans; } int n; point ttmp; point pt1[1001000],pt2[1001000]; bool cmpx(point xx,point yy){ if(cmp(xx.y-yy.y)==0) return xx.x<yy.x; return xx.y<yy.y; } bool cmpd(point xx, point yy){ double db=(xx-ttmp)^(yy-ttmp); if(cmp(db)==0) return dist(xx,ttmp)<dist(yy,ttmp); if(cmp(db)>0) return 1; else return 0; } point grp1[1001000],grp2[1001000]; int Graham(point* grp,point *pt,int n){ //凸包 int top=1; sort(pt,pt+n,cmpx);ttmp=pt[0]; sort(pt+1,pt+n,cmpd); grp[0]=pt[0]; grp[1]=pt[1]; for(int i=2;i<n;i++){ while(top>0){ double db=(pt[i]-grp[top])^(grp[top]-grp[top-1]); if(cmp(db)>=0) top--; else break; } grp[++top]=pt[i]; } return top+1; } double rotating_calipers(point* grp ,int len){ //旋转卡壳求凸包直径 int i=0,j=1; double ans=0; while(i<len){ while(area(grp[i],grp[i+1],grp[(j+1)%len])> area(grp[i],grp[i+1],grp[j])) j=(j+1)%len; ans=max(ans,max(dist(grp[i],grp[j]),dist(grp[i+1],grp[j]))); i++; } return ans; } double rotating_calipers2(point* grp1,int len1,point* grp2,int len2){ //旋转卡壳 求两个凸包的最远距离 int p=0,q=0; for(int i=0;i<len1;i++) if(grp1[i].y<grp1[p].y) p=i; for(int i=0;i<len2;i++) if(grp2[i].y>grp2[q].y) q=i; double ans=1e99,tmp; grp1[len1]=grp1[0];//避免取模 grp2[len2]=grp2[0];//避免取模 for(int i=0;i<len1;i++){ while(tmp=cmp(area(grp1[p],grp1[p+1],grp2[q+1])- area(grp1[p],grp1[p+1],grp2[q]))>0) q=(q+1)%len2; if(tmp==0) ans=min(ans,dis_pall_seg(grp1[p],grp1[p+1],grp2[q],grp2[q+1])); else ans=min(ans,dis_p_to_seg(grp2[q],line(grp1[p],grp1[p+1]))); p=(p+1)%len1; } return ans; } double rotating_calipers3(point* grp ,int len){ //旋转卡壳求凸包宽度 int p=0,q=0; int tmp; for(int i=0;i<len;i++) if(grp[i].y<grp[p].y) p=i; for(int j=0;j<len;j++) if(grp[j].y>grp[q].y) q=j; double ans=1e30; for(int i=0;i<len;i++){ while(tmp=cmp(area(grp[p],grp[p+1],grp[(q+1)%len])- area(grp[p],grp[p+1],grp[q]))>0) q=(q+1)%len; ans=min(ans,dis_p_to_line(grp[q],line(grp[p],grp[(p+1)%len]))); p=(p+1)%len; } return ans; } double rotating_calipers4(point* grp,int len){ double ans; int xmin=0,xmax=0,ymin=0,ymax=0; for(int i=0;i<len;i++) if(cmp(grp[xmin].x-grp[i].x)>0) xmin=i; for(int i=0;i<len;i++) if(cmp(grp[xmax].x-grp[i].x)<0) xmax=i; for(int i=0;i<len;i++) if(cmp(grp[ymin].y-grp[i].y)>0) ymin=i; for(int i=0;i<len;i++) if(cmp(grp[ymax].y-grp[i].y)<0) ymax=i; ans=(grp[ymax].y-grp[ymin].y)*(grp[xmax].x-grp[xmin].x); grp[len]=grp[0]; for(int i=0;i<len;i++){ while(cmp(area(grp[ymin],grp[ymin+1],grp[ymax+1])- area(grp[ymin],grp[ymin+1],grp[ymax]))>=0) ymax=(ymax+1)%len; while(cmp(dot(grp[xmax+1],grp[ymin],grp[ymin+1])- dot(grp[xmax],grp[ymin],grp[ymin+1]))>=0) xmax=(xmax+1)%len; if(i==0) xmin=xmax; while(cmp(dot(grp[xmin+1],grp[ymin+1],grp[ymin])- dot(grp[xmin],grp[ymin+1],grp[ymin]))>=0) xmin=(xmin+1)%len; double L1=dis_p_to_line(grp[ymax],line(grp[ymin],grp[ymin+1])); point a=P_chuizhi_line(grp[xmin],grp[ymin],grp[ymin+1]); double L2=dis_p_to_line(grp[xmax],line(grp[xmin],a)); if(ans>L1*L2){ ans=L1*L2; } ymin=(ymin+1)%len; } return ans; } struct node{ double angle; }fuck[1000]; bool nodecmp(node a,node b){ return a.angle<b.angle; } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); #endif // ONLINE_JUDGE int t;cin>>t; while(t--){ scanf("%d",&n); for(int i=0;i<n;i++) pt1[i].input(); long long ans=1ll*n*(n-1)*(n-2)*(n-3)/24; for(int i=0;i<n;i++){ int cnt=0; for(int j=0;j<n;j++) if(i!=j){ fuck[cnt++].angle=(pt1[j]-pt1[i]).angle()+pi; } sort(fuck,fuck+cnt,nodecmp); for(int j=cnt;j<2*cnt;j++) fuck[j].angle=fuck[j-cnt].angle+2*pi; int st=1; long long tmp=0; for(int j=0;j<cnt;j++){ while(fuck[st].angle-fuck[j].angle<pi) st++; if(st-j-1<2) continue; tmp+=(st-j-1)*(st-j-2)/2; } ans-=(1ll*(n-1)*(n-2)*(n-3)/6-tmp); } printf("%lld\n",ans); } }
相关文章推荐
- poll服务端代码实现
- LVS-DR简单模型实现负载均衡
- Easyui 表格datagrid 的单元格内容显示方式扩展
- javascript封装简单实现方法
- 二维码扫描
- Android如何获取系统联系人数据?
- 算法 n大小的数组中找出出现3/n次数以上的元素
- UVA 11292 The Dragon of Loowater
- iOS入门(二十)字典
- 文件io操作
- 24-IO流-61-IO流(RandomAccessFile-随机写入&细节)
- 24-IO流-62-IO流(管道流-PipedStream)
- 设计模式之观察者模式
- 《深入了解mybatis原则》 MyBatis架构设计和案例研究
- SEO
- eclipse下svn的分支与合并指南 - 更新版
- C# 匿名方法 委托 Action委托 Delegate委托
- eclipse+maven+springmvc+tomcat 报找不到类的错误
- 详解PHP中的状态模式编程
- 2015.08.11 通过Excel拼装sql语句