Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.util.StringTokenizer;
  8. import java.util.Random;
  9. import java.io.PrintWriter;
  10. /*
  11. Solution Created: 18:48:28 16/10/2019
  12. Custom Competitive programming helper.
  13. */
  14.  
  15. public class Main {
  16. public static void solve(Reader in, Writer out) {
  17. int n = in.nextInt();
  18. Point[] a = new Point[n];
  19. for(int i = 0; i<n; i++) a[i] = new Point(i+1, in.nextInt(), in.nextInt(), in.nextInt());
  20. boolean[] used = new boolean[n+2];
  21. Pair[] ps = new Pair[n*(n-1)/2];
  22. int idx = 0;
  23. for(int i = 0; i<n; i++) for(int j = i+1; j<n; j++) ps[idx++] = new Pair(a[i], a[j]);
  24. Arrays.sort(ps);
  25. int printed = 0;
  26. for(int i = 0; i<n*(n-1)/2; i++) {
  27. Pair p = ps[i];
  28. if((!used[p.p1.idx])&&(!used[p.p2.idx])) {
  29. out.println(p.p1.idx+" "+p.p2.idx);
  30. used[p.p1.idx] = used[p.p2.idx] = true;
  31. printed++;
  32. }
  33. if(printed == n/2) break;
  34. }
  35. }
  36. static class Pair implements Comparable<Pair>{
  37. Point p1,p2;
  38. double dist;
  39. public Pair(Point p1, Point p2) {
  40. this.p1 = p1;
  41. this.p2 = p2;
  42. dist = Math.sqrt(Math.pow(p1.x-p2.x, 2) + Math.pow(p1.y-p2.y, 2) + Math.pow(p1.z-p2.z, 2));
  43. }
  44. @Override
  45. public int compareTo(Pair o) {
  46. return Double.compare(this.dist , o.dist);
  47. }
  48. }
  49. static class Point{
  50. int idx;
  51. double x,y,z;
  52. public Point(int idx, double x, double y, double z) {
  53. this.idx = idx;
  54. this.x = x;
  55. this.y = y;
  56. this.z = z;
  57. }
  58. }
  59. public static void main(String[] args) {
  60. Reader in = new Reader();
  61. Writer out = new Writer();
  62. solve(in, out);
  63. out.exit();
  64. }
  65.  
  66. static class Graph {
  67. private ArrayList<Integer> con[];
  68. private boolean[] visited;
  69.  
  70. public Graph(int n) {
  71. con = new ArrayList[n];
  72. for (int i = 0; i < n; ++i)
  73. con[i] = new ArrayList();
  74. visited = new boolean[n];
  75. }
  76.  
  77. public void reset() {
  78. Arrays.fill(visited, false);
  79. }
  80.  
  81. public void addEdge(int v, int w) {
  82. con[v].add(w);
  83. }
  84.  
  85. public void dfs(int cur) {
  86. visited[cur] = true;
  87. for (Integer v : con[cur]) {
  88. if (!visited[v]) {
  89. dfs(v);
  90. }
  91. }
  92. }
  93.  
  94. public void bfs(int cur) {
  95. Queue<Integer> q = new LinkedList<>();
  96. q.add(cur);
  97. visited[cur] = true;
  98. while (!q.isEmpty()) {
  99. cur = q.poll();
  100. for (Integer v : con[cur]) {
  101. if (!visited[v]) {
  102. visited[v] = true;
  103. q.add(v);
  104. }
  105. }
  106. }
  107. }
  108. }
  109.  
  110. static class Reader {
  111. static BufferedReader br;
  112. static StringTokenizer st;
  113. private int charIdx = 0;
  114. private String s;
  115.  
  116. public Reader() {
  117. this.br = new BufferedReader(new InputStreamReader(System.in));
  118. }
  119.  
  120. public int[] na(int n) {
  121. int[] a = new int[n];
  122. for (int i = 0; i < n; i++)
  123. a[i] = nextInt();
  124. return a;
  125. }
  126.  
  127. public double[] nd(int n) {
  128. double[] a = new double[n];
  129. for (int i = 0; i < n; i++)
  130. a[i] = nextDouble();
  131. return a;
  132. }
  133.  
  134. public char nextChar() {
  135. if (s == null || charIdx >= s.length()) {
  136. if (st == null || !st.hasMoreTokens())
  137. try {
  138. readLine();
  139. } catch (Exception e) {
  140. }
  141. s = st.nextToken();
  142. charIdx = 0;
  143. }
  144. return s.charAt(charIdx++);
  145. }
  146.  
  147. public long[] nl(int n) {
  148. long[] a = new long[n];
  149. for (int i = 0; i < n; i++)
  150. a[i] = nextLong();
  151. return a;
  152. }
  153.  
  154. public char[] nca() {
  155. return next().toCharArray();
  156. }
  157.  
  158. public String[] nS(int n) {
  159. String[] a = new String[n];
  160. for (int i = 0; i < n; i++)
  161. a[i] = next();
  162. return a;
  163. }
  164.  
  165. public int nextInt() {
  166. if (st == null || !st.hasMoreTokens())
  167. try {
  168. readLine();
  169. } catch (Exception e) {
  170. }
  171. return Integer.parseInt(st.nextToken());
  172. }
  173.  
  174. public double nextDouble() {
  175. if (st == null || !st.hasMoreTokens())
  176. try {
  177. readLine();
  178. } catch (Exception e) {
  179. }
  180. return Double.parseDouble(st.nextToken());
  181. }
  182.  
  183. public Long nextLong() {
  184. if (st == null || !st.hasMoreTokens())
  185. try {
  186. readLine();
  187. } catch (Exception e) {
  188. }
  189. return Long.parseLong(st.nextToken());
  190. }
  191.  
  192. public String next() {
  193. if (st == null || !st.hasMoreTokens())
  194. try {
  195. readLine();
  196. } catch (Exception e) {
  197. }
  198. return st.nextToken();
  199. }
  200.  
  201. public static void readLine() {
  202. try {
  203. st = new StringTokenizer(br.readLine());
  204. } catch (Exception e) {
  205. }
  206. }
  207. }
  208.  
  209. static class Sort {
  210. static Random random = new Random();
  211. public static void sortArray(int[] s) {
  212. shuffle(s);
  213. Arrays.sort(s);
  214. }
  215. public static void sortArray(long[] s) {
  216. shuffle(s);
  217. Arrays.sort(s);
  218. }
  219. public static void sortArray(String[] s) {
  220. shuffle(s);
  221. Arrays.sort(s);
  222. }
  223. public static void sortArray(char[] s) {
  224. shuffle(s);
  225. Arrays.sort(s);
  226. }
  227. private static void shuffle(int[] s) {
  228. for (int i = 0; i < s.length; ++i) {
  229. int j = random.nextInt(i + 1);
  230. int t = s[i];
  231. s[i] = s[j];
  232. s[j] = t;
  233. }
  234. }
  235. private static void shuffle(long[] s) {
  236. for (int i = 0; i < s.length; ++i) {
  237. int j = random.nextInt(i + 1);
  238. long t = s[i];
  239. s[i] = s[j];
  240. s[j] = t;
  241. }
  242. }
  243. private static void shuffle(String[] s) {
  244. for (int i = 0; i < s.length; ++i) {
  245. int j = random.nextInt(i + 1);
  246. String t = s[i];
  247. s[i] = s[j];
  248. s[j] = t;
  249. }
  250. }
  251. private static void shuffle(char[] s) {
  252. for (int i = 0; i < s.length; ++i) {
  253. int j = random.nextInt(i + 1);
  254. char t = s[i];
  255. s[i] = s[j];
  256. s[j] = t;
  257. }
  258. }
  259. }
  260.  
  261. static class Util{
  262. static boolean isPrime(int n) {
  263. if (n <= 1) return false;
  264. if (n <= 3) return true;
  265. if (n % 2 == 0 || n % 3 == 0) return false;
  266. for (int i = 5; i * i <= n; i = i + 6)
  267. if (n % i == 0 || n % (i + 2) == 0)
  268. return false;
  269. return true;
  270. }
  271. public static int upperBound(long[] a, long v) {
  272. int l=0, h=a.length-1, ans = -1;
  273. while(l<h) {
  274. int mid = (l+h)/2;
  275. if(a[mid]<=v) {
  276. ans = mid;
  277. l = mid+1;
  278. }else h = mid-1;
  279. }
  280. return ans;
  281. }
  282. public static int lowerBound(long[] a, long v) {
  283. int l=0, h=a.length-1, ans = -1;
  284. while(l<h) {
  285. int mid = (l+h)/2;
  286. if(v<=a[mid]) {
  287. ans = mid;
  288. h = mid-1;
  289. }else l = mid-1;
  290. }
  291. return ans;
  292. }
  293. public static boolean[] getSieve(int n) {
  294. boolean[] isPrime = new boolean[n+1];
  295. for (int i = 2; i <= n; i++) isPrime[i] = true;
  296. for (int i = 2; i*i <= n; i++) if (isPrime[i])
  297. for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
  298. return isPrime;
  299. }
  300. public static int gcd(int a, int b) {
  301. if (a == 0)
  302. return b;
  303. return gcd(b%a, a);
  304. }
  305. public static long modAdd(long a, long b, long mod) {
  306. return (a%mod+b%mod)%mod;
  307. }
  308. public static long modMul(long a, long b, long mod) {
  309. return ((a%mod)*(b%mod))%mod;
  310. }
  311. public static void dbg(Object... o) {
  312. System.out.println(Arrays.deepToString(o));
  313. }
  314. }
  315.  
  316. static class Writer {
  317. private PrintWriter pw;
  318. public Writer(){
  319. pw = new PrintWriter(System.out);
  320. }
  321.  
  322. public void printArray(int[] a) {
  323. for(int i = 0; i<a.length; i++) print(a[i]+" ");
  324. }
  325. public void printlnArray(int[] a) {
  326. for(int i = 0; i<a.length; i++) print(a[i]+" ");
  327. pw.println();
  328. }
  329. public void printArray(long[] a) {
  330. for(int i = 0; i<a.length; i++) print(a[i]+" ");
  331. }
  332. public void printlnArray(long[] a) {
  333. for(int i = 0; i<a.length; i++) print(a[i]+" ");
  334. pw.println();
  335. }
  336. public void print(Object o) {
  337. pw.print(o.toString());
  338. }
  339. public void println(Object o) {
  340. pw.println(o.toString());
  341. }
  342. public void println() {
  343. pw.println();
  344. }
  345. public void flush() {
  346. pw.flush();
  347. }
  348. public void exit() {
  349. pw.close();
  350. }
  351. }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement