递归版二分查找,最慢的一种了, ZOJ 跑了4800ms+ 才过,POJ 直接 TL ,掩面啊,泪奔啊……
#include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; #define N 1005 struct point { double x,y; }; point p[N]; int n; bool operator ==(point a,point b)//运算符重载 { if( a.x==b.x && a.y==b.y )return true; return false; } bool operator >(point a,point b) { if(a.x>b.x || a.x==b.x && a.y>b.y)return true; return false; } bool operator <(point a,point b) { if(a.x<b.x || a.x==b.x && a.y<b.y)return true; return false; } int cmp(const void *a,const void *b) { return *(point *)a > *(point *)b ? 1:-1; } int find(point p[],int low,int high,point v)//递归的二分 { if(low==high)return -1; if(p[low] == v)return low; if(p[high] == v)return high; int mid=(low+high)/2; if( p[mid] == v ) return mid; if( v > p[mid] ) return find(p,mid+1,high,v); return find(p,low,mid,v); } point Whirl(double cosl, double sinl, point a, point b) // ab 绕 a 逆时针转过角度 A 得到的点,sinl=sinA,cosl=cosA { 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; } int judge(int i,int j) { point pa,pb,po; if(i==j)return 0; po.x=(p[i].x+p[j].x)/2.0; po.y=(p[i].y+p[j].y)/2.0; pa=Whirl(0.0,1.0,po,p[i]);//逆时针90度旋转 pb=Whirl(0.0,1.0,po,p[j]);//顺时针旋转 if(find(p,0,n,pa)==-1) return 0; else if(find(p,0,n,pb)==-1) return 0; return 1; } int main() { int t,i,j; scanf("%d",&t); while(t--) { while(scanf("%d",&n),n) { for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); qsort(p,n,sizeof(p[0]),cmp); int cnt=0; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) if(judge(i,j)==1)cnt++; } printf("%d\n",cnt/2); } if(t)puts(""); } return 0; }
#include<iostream> #include<stdio.h> #include<stdlib.h> using namespace std; #define N 1005 struct point { double x,y; }; point p[N]; int n; bool operator ==(point a,point b)//运算符重载 { if( a.x==b.x && a.y==b.y )return true; return false; } bool operator >(point a,point b) { if(a.x>b.x || a.x==b.x && a.y>b.y)return true; return false; } bool operator <(point a,point b) { if(a.x<b.x || a.x==b.x && a.y<b.y)return true; return false; } int cmp(const void *a,const void *b) { return *(point *)a > *(point *)b ? 1:-1; } bool find(point a,int m) { int begin = 0,end = m-1; while( begin <= end ) { int mid = ( begin + end )/2; if( a.x==p[mid].x && a.y==p[mid].y )return true; if( a.x>p[mid].x || a.x==p[mid].x && a.y>p[mid].y ) begin = mid + 1; else end = mid - 1; } return false; } point Whirl(double cosl, double sinl, point a, point b) // ab 绕 a 逆时针转过角度 A 得到的点,sinl=sinA,cosl=cosA { 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; } int judge(int i,int j) { point pa,pb,po; if(i==j)return 0; po.x=(p[i].x+p[j].x)/2.0; po.y=(p[i].y+p[j].y)/2.0; pa=Whirl(0.0,1.0,po,p[i]);//逆时针90度旋转 pb=Whirl(0.0,1.0,po,p[j]);//顺时针旋转 if(!find(pa,n)) return 0; else if(!find(pb,n)) return 0; return 1; } int main() { int i,j; while(scanf("%d",&n),n) { for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y); qsort(p,n,sizeof(p[0]),cmp); int cnt=0; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) if(judge(i,j)==1)cnt++; } printf("%d\n",cnt/2); } return 0; }
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; struct node { int x, y; } p[1001]; bool op(node xx,node yy) { if (xx.x == yy.x) return xx.y < yy.y; else return xx.x < yy.x; } int main() { int points; while (scanf("%d", &points), points) { int sum = 0; for (int k = 0; k < points; ++k) scanf("%d%d", &p[k].x, &p[k].y); sort(p, p + points, op); for (int i = 0; i < points; ++i) { for (int j = i + 1; j < points; ++j) { if(p[i].x <= p[j].x && p[i].y >= p[j].y) { node p0, p1; p0.x = p[i].x + p[j].y - p[i].y; p0.y = p[i].y + p[i].x - p[j].x; p1.x = p[j].x + p[j].y - p[i].y; p1.y = p[j].y + p[i].x - p[j].x; if (!binary_search(p, p+points, p0, op)) continue; if (!binary_search(p, p+points, p1, op)) continue; sum++; } } } printf("%d\n", sum); } return 0; }