import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] words = in.readLine().split("\\s+"); int n, m; n = Integer.valueOf(words[0]); m = Integer.valueOf(words[1]); words = in.readLine().split("\\s+"); int[] arr = new int[n]; for (int i = 0; i < words.length; i++) { arr[i] = Integer.valueOf(words[i]); } int[][][] dp = new int[n+1][m + 1][2]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { dp[i][j][0] = 1000000000; dp[i][j][1] = 1000000000; } } dp[1][0][0] = 1; if (arr[0] <= m) { dp[1][arr[0]][1] = 0; } for (int i = 2; i <= n; i++) { dp[i][0][0] = 1; for (int j = 1; j <= m; j++) { dp[i][j][0] = Math.min(dp[i - 1][j][0], dp[i - 1][j][1] + 1); if (arr[i - 1] <= j) { dp[i][j][1] = dp[i - 1][j - arr[i-1]][0]; dp[i][j][1] = Math.min(dp[i][j][1], dp[i - 1][j - arr[i - 1]][0]); dp[i][j][1] = Math.min(dp[i][j][1], dp[i - 1][j - arr[i - 1]][1]); } } } for (int j = 1; j <= m; j++) { int x = Math.min(dp[n][j][0],dp[n][j][1]); System.out.println(x>=1000000000?-1:x); } in.close(); } }
标签:atcoder,java,io,int,Subarrays,valueOf,Erase,words,new From: https://www.cnblogs.com/fishcanfly/p/16843878.html