fork download
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String args[]) {
  7. // staticメソッドからだと、内部クラスが呼び出せないらしい。なぜ?
  8. Main m = new Main();
  9. m.init();
  10. try {
  11. m.calc();
  12. m.print();
  13. } catch (Exception e) {
  14. m.print();
  15. e.printStackTrace();
  16. }
  17.  
  18. }
  19.  
  20. int L = 30;
  21. int maxNum = 10;
  22. private int[] A;// スタート地点からの距離、A[0]=0とする
  23. private int[] B;// 給油可能量、B[0]はスタート地点で給油可能な量
  24.  
  25. ArrayList<int[]> array = new ArrayList<int[]>();// 本当はハッシュにしたいけど、もう夜遅いので・・・
  26.  
  27. private void print() {
  28.  
  29. for (int l = 0; l < array.size(); l++) {
  30. int[] vec = array.get(l);
  31. System.out.printf("l=%4d: ", l);
  32. printIntVec(vec);
  33. System.out.println();
  34. }
  35. }
  36.  
  37. private void printIntVec(int[] vec) {
  38. for (int i = 0; i < vec.length; i++) {
  39. if (i != 0)
  40. System.out.print(", ");
  41. System.out.printf("%2d", vec[i]);
  42. }
  43. }
  44.  
  45. private void init() {
  46. Random rand = new Random();
  47. A = new int[maxNum];
  48. B = new int[maxNum];
  49. for (int i = 0; i < maxNum; i++) {
  50. if (i == 0) {
  51. A[0] = 0;
  52. } else {
  53. A[i] = A[i - 1] + 1 + rand.nextInt(2 * L / maxNum);
  54. }
  55. B[i] = 1 + rand.nextInt(3 * L / maxNum);
  56. }
  57. System.out.print(" ");
  58. for (int i = 0; i < maxNum; i++) {
  59. if (i != 0)
  60. System.out.print(", ");
  61. System.out.printf("%2d", i);
  62. }
  63. System.out.println();
  64.  
  65. System.out.print("A:");
  66. printIntVec(A);
  67. System.out.println();
  68.  
  69. System.out.print("B:");
  70. printIntVec(B);
  71. System.out.println();
  72. System.out.printf("L:%2d\n", L);
  73. }
  74.  
  75. private void calc() throws Exception {
  76. for (int l = 0; l <= L; l++) {
  77. // 初期化
  78. if (l == 0) {
  79. int[] cur = new int[B[0] + 1];
  80. for (int i = 0; i < cur.length; i++) {
  81. cur[i] = 1;// 1回だけ給油した結果、燃料をiだけ持ってます
  82. }
  83. array.add(cur);
  84. } else {
  85. int fuelObtained = possibleFuel(l);
  86. int[] prev = array.get(l - 1);
  87. if (prev.length - 1 + fuelObtained <= 0)
  88. throw new Exception("cannot reach at L");
  89. int[] cur = new int[prev.length - 1 + fuelObtained];
  90.  
  91. // 給油しない場合
  92. for (int i = 0; i < cur.length; i++) {
  93. if (i + 1 < prev.length) {
  94. cur[i] = prev[i + 1];
  95. } else {
  96. cur[i] = -1;// 未確定
  97. }
  98. }
  99. // 給油する場合
  100. for (int i = 0; i < prev.length; i++) {
  101. for (int j = 1; j <= fuelObtained; j++) {
  102. if (i + j < cur.length) {
  103. if (cur[i + j] == -1
  104. || prev[i] + 1 < cur[i + j]) {
  105. cur[i + j] = prev[i] + 1;
  106. }
  107. }
  108. }
  109. }
  110. array.add(cur);
  111. }
  112. }
  113. }
  114.  
  115. private int possibleFuel(int dist) {
  116. int tmp = 0;
  117. for (int i = 0; i < maxNum; i++) {
  118. if (A[i] == dist)
  119. tmp += B[i];
  120. }
  121. return tmp;
  122. }
  123.  
  124. }
  125.  
Success #stdin #stdout 0.11s 245952KB
stdin
Standard input is empty
stdout
   0,  1,  2,  3,  4,  5,  6,  7,  8,  9
A: 0,  1,  3,  7, 11, 16, 17, 21, 26, 32
B: 9,  5,  2,  7,  7,  9,  3,  6,  2,  5
L:30
l=   0:  1,  1,  1,  1,  1,  1,  1,  1,  1,  1
l=   1:  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2
l=   2:  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2
l=   3:  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3
l=   4:  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3
l=   5:  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3
l=   6:  1,  1,  1,  1,  2,  2,  2,  2,  2,  3,  3
l=   7:  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  4
l=   8:  1,  1,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  4
l=   9:  1,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  4
l=  10:  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  4
l=  11:  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4
l=  12:  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4
l=  13:  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4
l=  14:  2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4
l=  15:  2,  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4
l=  16:  2,  2,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5
l=  17:  2,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6
l=  18:  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6
l=  19:  3,  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6
l=  20:  3,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6
l=  21:  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7
l=  22:  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7
l=  23:  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7
l=  24:  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7
l=  25:  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7
l=  26:  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  8
l=  27:  3,  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  8
l=  28:  4,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  8
l=  29:  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  8
l=  30:  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  5,  6,  6,  6,  6,  7,  7,  7,  8