Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <ctime>
- #include <memory.h>
- #include <algorithm>
- #include <cstdio>
- #include <queue>
- #include <cmath>
- using namespace std;
- int x[30002],y[30002],z[30002];
- vector < pair <double,int> > v;
- int ansp[30002];
- long long dist(int i,int j) {
- return 1LL*(x[i]-x[j])*(x[i]-x[j])+1LL*(y[i]-y[j])*(y[i]-y[j])+1LL*(z[i]-z[j])*(z[i]-z[j]);
- }
- int main() {
- freopen("j8.in","r",stdin);
- freopen("j8.out","w",stdout);
- int n;
- scanf("%d",&n);
- for(int i=0;i<n;++i)
- scanf("%d%d%d",&x[i],&y[i],&z[i]);
- srand(time(NULL));
- double A=rand(),B=rand(),C=rand(),g=sqrt(0.+A*A+B*B+C*C);
- A/=g; B/=g; C/=g;
- for(int i=0;i<n;++i)
- v.push_back(make_pair(A*x[i]+B*y[i]+C*z[i],i));
- sort(v.begin(),v.end());
- for(int i=0;i<n;++i) {
- long long ans=0; int ind;
- if (i) ans=dist(v[i].second,v[i-1].second),ind=i-1;
- else ans=dist(v[i].second,v[i+1].second),ind=i+1;
- int l=i-1,r=i+1;
- while(true) {
- bool ok=0;
- if (l>=0&&(v[i].first-v[l].first)*(v[i].first-v[l].first)<ans) {
- long long cur=dist(v[i].second,v[l].second);
- if (cur<ans) ans=cur,ind=l;
- --l; ok=1;
- }
- if (r<n&&(v[r].first-v[i].first)*(v[r].first-v[i].first)<ans) {
- long long cur=dist(v[i].second,v[r].second);
- if (cur<ans) ans=cur,ind=r;
- ++r; ok=1;
- }
- if (!ok) break;
- }
- ansp[v[i].second]=v[ind].second;
- }
- for(int i=0;i<n;++i) {
- if (i) printf(" ");
- printf("%d",ansp[i]+1);
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement