随便写了写不知道对不对……
2333333
主要:找到最下边最左边的点,与剩余的点形成角度,对角度排序,如果剩余点和最开始的点对应(巨人与鬼对应)则完成配对,否者,按照角度从小到大寻找,巨人与鬼个数一样则完成配对,进行下一轮……22222
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <cstdlib> #include <stack> #include <cctype> #include <string> #include <malloc.h> #include <queue> #include <map> using namespace std; const int INF = 0xffffff; const double esp = 10e-8; const double Pi = 4 * atan(1.0); const int Maxn = 2000+10; const long long mod = 2147483647; const int dr[] = {1,0,-1,0,-1,1,-1,1}; const int dc[] = {0,1,0,-1,1,-1,-1,1}; typedef long long LL; LL gac(LL a,LL b){ return b?gac(b,a%b):a; } struct guest{ double x,y,angle; int v; int num1; int num2; bool operator < (const guest a)const{ if(abs(y-a.y) < esp) return x < a.x; return y < a.y; } }; bool cmp(const guest a,const guest b){ double x = a.angle; double y = b.angle; // cout << x << ‘ ‘ << y << ‘ ‘; if((x > 0 && y > 0) || (x < 0 && y < 0)|| (abs(x) < esp && y > 0)||(abs(y) <esp && x > 0)){ // cout << (x < y) << endl; return (x < y); } // cout << !(x < y) << ‘!‘ << endl; return !(x < y); } double get_angle(guest a,guest b){ if(abs(a.y - b.y) < esp){ if(b.x - a.x > 0) //不可能完全重合 return 0; else return -1*esp*100; } if(abs(a.x - b.x) < esp) return 0xfffffff; return (a.y-b.y)/(a.x-b.x); } guest arr[1000]; void dis(int s,int e){ if(e-s == 1){ arr[s].num2 = arr[e].num1; arr[e].num2 = arr[s].num1; return; } sort(arr+s,arr+e+1); int sum = arr[s].v; for(int i = s+1;i <= e;i++){ arr[i].angle = get_angle(arr[s],arr[i]); } sort(arr+s+1,arr+e+1,cmp); int t = s; while(sum){ sum += arr[++t].v; } dis(s,t); dis(t+1,e); } int main() { #ifndef ONLINE_JUDGE freopen("inpt.txt","r",stdin); #endif int n; while(~scanf("%d",&n)){ for(int i = 0;i < n;i++){ scanf("%d%lf%lf",&arr[i].num1,&arr[i].x,&arr[i].y); arr[i].v = 1; arr[i].num2 = 0; } for(int i = n;i < 2*n;i++){ scanf("%d%lf%lf",&arr[i].num1,&arr[i].x,&arr[i].y); arr[i].v = -1; arr[i].num2 = 0; } dis(0,2*n-1); for(int i = 0;i < 2*n;i++){ if(arr[i].v > 0){ cout << arr[i].num1 << ‘ ‘ << arr[i].num2 << endl; } } } return 0; }
时间: 2024-11-05 16:31:05