結果

問題 No.10 +か×か
ユーザー uafr_csuafr_cs
提出日時 2015-09-14 14:55:45
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,079 ms / 5,000 ms
コード長 1,243 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 85,720 KB
実行使用メモリ 107,812 KB
最終ジャッジ日時 2023-08-21 06:14:15
合計ジャッジ時間 7,359 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,080 KB
testcase_01 AC 122 ms
56,112 KB
testcase_02 AC 123 ms
55,596 KB
testcase_03 AC 1,010 ms
102,152 KB
testcase_04 AC 248 ms
59,788 KB
testcase_05 AC 127 ms
55,828 KB
testcase_06 AC 1,079 ms
107,812 KB
testcase_07 AC 561 ms
92,348 KB
testcase_08 AC 268 ms
63,820 KB
testcase_09 AC 249 ms
64,248 KB
testcase_10 AC 127 ms
56,052 KB
testcase_11 AC 123 ms
56,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		final int T = sc.nextInt();
		
		int[] A = new int[N];
		for(int i = 0; i < N; i++){
			A[i] = sc.nextInt();
		}
		
		String[] DP = new String[T + 1];
		String[] next_DP = new String[T + 1];
		DP[A[0]] = "";
		
		for(int i = 1; i < N; i++){
			Arrays.fill(next_DP, null);
			
			for(int j = T - A[i]; j >= 0; j--){
				if(DP[j] == null){ continue; }
				
				final String next_str = DP[j] + "+";
				
				if(next_DP[j + A[i]] == null){
					next_DP[j + A[i]] = next_str;
				}else if(next_str.compareTo(next_DP[j + A[i]]) < 0){
					next_DP[j + A[i]] = next_str;
				}
			}
			
			for(int j = T / A[i]; j > 0; j--){
				if(DP[j] == null){ continue; }
				
				final String next_str = DP[j] + "*";
				
				if(next_DP[j * A[i]] == null){
					next_DP[j * A[i]] = next_str;
				}else if(next_str.compareTo(next_DP[j * A[i]]) > 0){
					next_DP[j * A[i]] = next_str;
				}
			}
			
			String[] tmp = DP;
			DP = next_DP;
			next_DP = tmp;
		}
		
		System.out.println(DP[T]);
	}

}
0