|
| 1 | +// chatgpt |
| 2 | +import java.util.Arrays; |
| 3 | + |
| 4 | +public class StampOptimization { |
| 5 | + |
| 6 | + private static int maxValue = 0; |
| 7 | + private static int[] bestStamps = new int[10]; |
| 8 | + private static int H, K; |
| 9 | + private static int[] currStamps = new int[10]; |
| 10 | + private static int[][] dp = new int[200][10]; |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + java.util.Scanner scanner = new java.util.Scanner(System.in); |
| 14 | + |
| 15 | + while (true) { |
| 16 | + H = scanner.nextInt(); |
| 17 | + K = scanner.nextInt(); |
| 18 | + if (H == 0 && K == 0) break; |
| 19 | + |
| 20 | + // Initialize the dp table |
| 21 | + for (int[] row : dp) { |
| 22 | + Arrays.fill(row, 0); |
| 23 | + } |
| 24 | + dp[0][0] = 1; |
| 25 | + maxValue = 0; |
| 26 | + |
| 27 | + // Start the DFS to find the optimal denominations |
| 28 | + dfs(0, 0, 0); |
| 29 | + |
| 30 | + // Output the result |
| 31 | + for (int i = 0; i < K; i++) { |
| 32 | + System.out.printf("%3d", bestStamps[i]); |
| 33 | + } |
| 34 | + System.out.printf(" ->%3d%n", maxValue); |
| 35 | + } |
| 36 | + scanner.close(); |
| 37 | + } |
| 38 | + |
| 39 | + private static void dfs(int idx, int lastStamp, int maxReach) { |
| 40 | + int currMax = 0; |
| 41 | + |
| 42 | + // Calculate the maximum consecutive value that can be reached |
| 43 | + for (int i = maxReach; ; i++) { |
| 44 | + int j; |
| 45 | + for (j = 0; j <= H; j++) { |
| 46 | + if (dp[i][j] != 0) break; |
| 47 | + } |
| 48 | + if (j == H + 1) { |
| 49 | + currMax = i - 1; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Check if we have selected all denominations |
| 55 | + if (idx == K) { |
| 56 | + if (currMax >= maxValue) { |
| 57 | + maxValue = currMax; |
| 58 | + System.arraycopy(currStamps, 0, bestStamps, 0, K); |
| 59 | + } |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Try adding a new denomination |
| 64 | + int[][] steps = new int[10000][2]; |
| 65 | + for (int i = lastStamp + 1; i <= currMax + 1; i++) { |
| 66 | + int stepCount = 0; |
| 67 | + |
| 68 | + // Update dp table with the current denomination |
| 69 | + for (int j = 0; j <= 100; j++) { |
| 70 | + for (int k = 0; k < H; k++) { |
| 71 | + if (dp[j + i][k + 1] == 0 && dp[j][k] != 0) { |
| 72 | + dp[j + i][k + 1] = 1; |
| 73 | + steps[stepCount][0] = j + i; |
| 74 | + steps[stepCount][1] = k + 1; |
| 75 | + stepCount++; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + currStamps[idx] = i; |
| 81 | + dfs(idx + 1, i, currMax); |
| 82 | + |
| 83 | + // Backtrack to undo dp updates |
| 84 | + for (int j = 0; j < stepCount; j++) { |
| 85 | + dp[steps[j][0]][steps[j][1]] = 0; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments