Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2013
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <ctime>
  5. #include <memory.h>
  6. #include <algorithm>
  7. #include <cstdio>
  8. #include <queue>
  9. #include <cmath>
  10.  
  11. using namespace std;
  12.  
  13. int x[30002],y[30002],z[30002];
  14. vector < pair <double,int> > v;
  15. int ansp[30002];
  16.  
  17. long long dist(int i,int j) {
  18. 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]);
  19. }
  20.  
  21. int main() {
  22. freopen("j8.in","r",stdin);
  23. freopen("j8.out","w",stdout);
  24. int n;
  25. scanf("%d",&n);
  26. for(int i=0;i<n;++i)
  27. scanf("%d%d%d",&x[i],&y[i],&z[i]);
  28. srand(time(NULL));
  29. double A=rand(),B=rand(),C=rand(),g=sqrt(0.+A*A+B*B+C*C);
  30. A/=g; B/=g; C/=g;
  31. for(int i=0;i<n;++i)
  32. v.push_back(make_pair(A*x[i]+B*y[i]+C*z[i],i));
  33. sort(v.begin(),v.end());
  34. for(int i=0;i<n;++i) {
  35. long long ans=0; int ind;
  36. if (i) ans=dist(v[i].second,v[i-1].second),ind=i-1;
  37. else ans=dist(v[i].second,v[i+1].second),ind=i+1;
  38. int l=i-1,r=i+1;
  39. while(true) {
  40. bool ok=0;
  41. if (l>=0&&(v[i].first-v[l].first)*(v[i].first-v[l].first)<ans) {
  42. long long cur=dist(v[i].second,v[l].second);
  43. if (cur<ans) ans=cur,ind=l;
  44. --l; ok=1;
  45. }
  46. if (r<n&&(v[r].first-v[i].first)*(v[r].first-v[i].first)<ans) {
  47. long long cur=dist(v[i].second,v[r].second);
  48. if (cur<ans) ans=cur,ind=r;
  49. ++r; ok=1;
  50. }
  51. if (!ok) break;
  52. }
  53. ansp[v[i].second]=v[ind].second;
  54. }
  55. for(int i=0;i<n;++i) {
  56. if (i) printf(" ");
  57. printf("%d",ansp[i]+1);
  58. }
  59. printf("\n");
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement