fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Scanner in = new Scanner(System.in);
  10. TreeMap<Double, String> map = new TreeMap<>();
  11. int n = in.nextInt();
  12. for(int i = 1; i <= n; i++) {
  13. double item = in.nextDouble();
  14. if(map.get(item) == null) map.put(item, Integer.toString(i));
  15. else {
  16. String str = map.get(item);
  17. str += Integer.toString(i);
  18. map.put(item, str);
  19. }
  20. }
  21. while(map.size() != 1) {
  22. double x = map.firstKey();
  23. String strX = map.get(x);
  24. char numx = '0';
  25. if(strX.length() == 1) {
  26. map.remove(x);
  27. numx = strX.charAt(0);
  28. }
  29. else {
  30. numx = strX.charAt(0);
  31. map.put(x, strX.substring(1));
  32. }
  33. double y = map.firstKey();
  34. String strY = map.get(y);
  35. char numy = '0';
  36. if(strY.length() == 1) {
  37. map.remove(y);
  38. numy = strY.charAt(0);
  39. }
  40. else {
  41. numy = strY.charAt(0);
  42. map.put(y, strY.substring(1));
  43. }
  44. int numXInt = Character.getNumericValue(numx);
  45. int numYInt = Character.getNumericValue(numy);
  46. System.out.println(Math.min(numXInt, numYInt) + " " + Math.max(numXInt, numYInt));
  47. double newKey = (x + y) / 2;
  48. if(map.get(newKey) == null) map.put(newKey, Integer.toString(++n));
  49. else {
  50. String str = map.get(newKey);
  51. str += Integer.toString(++n);
  52. map.put(newKey, str);
  53. }
  54. }
  55. }
  56. }
Success #stdin #stdout 0.09s 2184192KB
stdin
5
234 2 5 54 5
stdout
2 3
5 6
4 7
1 8