fork(2) download
  1. import java.util.LinkedList;
  2. import java.util.List;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. int n=12;
  7. long totalStart = System.nanoTime();
  8.  
  9. final List<int[]>[] cache = new List[n];
  10.  
  11. for (int chordes = 1; chordes <= n; chordes++) {
  12. long start = System.nanoTime();
  13. final int points = chordes * 2;
  14.  
  15. final List<int[]> partialResult = new LinkedList<int[]>();
  16. for (int pos = 1; pos < points; pos+=2) {
  17. int leftDist = pos-1;
  18. int rightDist = points-pos-1;
  19.  
  20. if (leftDist > 0) {
  21. List<int[]> left = cache[(leftDist / 2 - 1)];
  22. for (int[] lpart : left) {
  23. if (rightDist > 0) {
  24. List<int[]> right = cache[(rightDist / 2 - 1)];
  25.  
  26. for (int[] rpart: right) {
  27. partialResult.add(build(points, pos, lpart, rpart));
  28. }
  29. } else {
  30. partialResult.add(build(points, pos, lpart, null));
  31. }
  32. }
  33. } else if (rightDist > 0) {
  34. List<int[]> right = cache[(rightDist / 2 - 1)];
  35.  
  36. for (int[] rpart: right) {
  37. partialResult.add(build(points, pos, null, rpart));
  38. }
  39. } else {
  40. partialResult.add(build(points, pos, null, null));
  41. }
  42. }
  43. cache[chordes-1] = partialResult;
  44. System.out.println(chordes + "\t" + (System.nanoTime() - start) + " ns");
  45. }
  46.  
  47. final List<int[]> result = cache[n-1];
  48. System.out.println("Total solutions: " + result.size());
  49. /*for (int[] r : result) {
  50.   for (int p : r) {
  51.   System.out.print(""+p+",");
  52.   }
  53.   System.out.println();
  54.   } */
  55.  
  56. System.out.println("Total time: " + (System.nanoTime() - totalStart)/1000000 + " ms");
  57. }
  58.  
  59. private static int[] build(int points, int pos, int[] lpart, int[] rpart) {
  60. int[] r = new int[points];
  61. r[0] = pos;
  62. r[pos] = 0;
  63. if (lpart != null) {
  64. int leftDist = pos-1;
  65. System.arraycopy(lpart, 0, r, 1, leftDist);
  66. }
  67. if (rpart != null) {
  68. int rightDist = points-pos-1;
  69. System.arraycopy(rpart, 0, r, pos + 1, rightDist);
  70. }
  71. return r;
  72. }
  73.  
  74. }
  75.  
Success #stdin #stdout 0.41s 320832KB
stdin
Standard input is empty
stdout
1	814034 ns
2	313475 ns
3	19786 ns
4	35096 ns
5	84491 ns
6	235656 ns
7	1052001 ns
8	3050483 ns
9	2163072 ns
10	10718426 ns
11	39851060 ns
12	246902430 ns
Total solutions: 208012
Total time: 306 ms