先排序,把所有x坐标相同的放到同一个vector里去,然后对每个vector里的都是二维点,很好求,然后最后再把剩下的对匹配了
#include<bits/stdc++.h> #include<vector> using namespace std; #define N 100005 struct Node{int x,y,z,id;}p[N]; int cmp1(Node & a,Node & b){ if(a.y==b.y)return a.z<b.z; return a.y<b.y; } int n; map<int,vector<Node> >mp; map<int,vector<Node> >::iterator it; vector<Node>Left; void calc(vector<Node> &v){//所有x相等的点 vector<Node> left; left.clear(); sort(v.begin(),v.end(),cmp1); for(int i=0;i<v.size();){ if(i+1<v.size()){ if(v[i].y==v[i+1].y){ cout<<v[i].id<<" "<<v[i+1].id<<' '; i+=2; } else { left.push_back(v[i]); i++; } } else { left.push_back(v[i]); break; } } while(left.size()>=2){ cout<<left.back().id<<" "; left.pop_back(); cout<<left.back().id<<" "; left.pop_back(); } if(left.size()) Left.push_back(left.back()); } int main(){ cin>>n; for(int i=1;i<=n;i++){ scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z); p[i].id=i; mp[p[i].x].push_back(p[i]); } for(it=mp.begin();it!=mp.end();it++){ calc(it->second); } for(int i=1;i<Left.size();i+=2) cout<<Left[i].id<<" "<<Left[i-1].id<<" "; }