結果

問題 No.4 おもりと天秤
ユーザー jp_stejp_ste
提出日時 2016-02-01 16:38:29
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,093 bytes
コンパイル時間 2,098 ms
コンパイル使用メモリ 78,060 KB
実行使用メモリ 61,896 KB
最終ジャッジ日時 2023-10-21 18:30:20
合計ジャッジ時間 11,440 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
57,356 KB
testcase_01 AC 130 ms
57,308 KB
testcase_02 AC 136 ms
57,404 KB
testcase_03 AC 132 ms
57,488 KB
testcase_04 AC 132 ms
57,388 KB
testcase_05 AC 141 ms
57,464 KB
testcase_06 AC 131 ms
57,260 KB
testcase_07 AC 138 ms
57,416 KB
testcase_08 AC 139 ms
57,712 KB
testcase_09 AC 145 ms
57,536 KB
testcase_10 AC 142 ms
57,568 KB
testcase_11 AC 132 ms
57,276 KB
testcase_12 AC 131 ms
57,444 KB
testcase_13 AC 130 ms
57,488 KB
testcase_14 AC 130 ms
57,280 KB
testcase_15 AC 130 ms
57,336 KB
testcase_16 AC 132 ms
57,320 KB
testcase_17 AC 133 ms
57,304 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	
	static int N;
	static int list[];
	static boolean flag[];
	static int target;
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		list = new int[N];
		int sum = 0;
		for(int i=0; i<N; i++) {
			list[i] = scan.nextInt();
			sum += list[i];
		}
		scan.close();
		
		if(sum % 2 == 1) {
			System.out.println("impossible");
		} else {
			target = sum / 2;
			flag = new boolean[N];
			while(true) {
				if( solve(0, 0) ) {
					boolean savedFlag[] = flag.clone();
					if(solve(0, 0)) {
						System.out.println("possible");
						break;
					} else {
						flag = savedFlag;
					}
				} else {
					System.out.println("impossible");
					break;
				}
			}
		}
	}
	
	static boolean solve(int now, int firstI) {
		if(now > target) return false;

		if(now == target) {
			return true;
		}
		
		boolean find = false;
		for(int i=firstI; i<N; i++) {
			if(!flag[i]) {
				flag[i] = true;
				find = solve(now+list[i], i+1);
				if(find) break;
				flag[i] = false;
			}
		}
		return find;
	}
}
0