結果

問題 No.332 数列をプレゼントに
ユーザー ぴろずぴろず
提出日時 2015-12-25 01:47:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 956 ms / 2,000 ms
コード長 1,692 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 79,516 KB
実行使用メモリ 179,196 KB
最終ジャッジ日時 2023-08-25 21:14:40
合計ジャッジ時間 14,674 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,900 KB
testcase_01 AC 121 ms
55,832 KB
testcase_02 AC 120 ms
55,716 KB
testcase_03 AC 119 ms
55,864 KB
testcase_04 AC 126 ms
56,300 KB
testcase_05 AC 263 ms
69,924 KB
testcase_06 AC 257 ms
67,848 KB
testcase_07 AC 287 ms
78,492 KB
testcase_08 AC 323 ms
78,416 KB
testcase_09 AC 171 ms
56,532 KB
testcase_10 AC 173 ms
58,484 KB
testcase_11 AC 241 ms
67,624 KB
testcase_12 AC 213 ms
65,464 KB
testcase_13 AC 237 ms
68,092 KB
testcase_14 AC 239 ms
68,044 KB
testcase_15 AC 169 ms
58,392 KB
testcase_16 AC 133 ms
55,728 KB
testcase_17 AC 132 ms
55,472 KB
testcase_18 AC 462 ms
111,676 KB
testcase_19 AC 131 ms
58,204 KB
testcase_20 AC 177 ms
60,972 KB
testcase_21 AC 160 ms
58,772 KB
testcase_22 AC 170 ms
58,412 KB
testcase_23 AC 252 ms
67,748 KB
testcase_24 AC 248 ms
68,144 KB
testcase_25 AC 248 ms
68,112 KB
testcase_26 AC 242 ms
67,772 KB
testcase_27 AC 247 ms
68,012 KB
testcase_28 AC 244 ms
68,024 KB
testcase_29 AC 253 ms
68,000 KB
testcase_30 AC 246 ms
68,032 KB
testcase_31 AC 250 ms
68,092 KB
testcase_32 AC 249 ms
68,060 KB
testcase_33 AC 149 ms
55,864 KB
testcase_34 AC 168 ms
55,604 KB
testcase_35 AC 956 ms
179,196 KB
testcase_36 AC 122 ms
56,248 KB
testcase_37 AC 131 ms
55,564 KB
testcase_38 AC 131 ms
55,592 KB
testcase_39 AC 133 ms
55,860 KB
testcase_40 AC 139 ms
55,392 KB
testcase_41 AC 143 ms
56,004 KB
testcase_42 AC 134 ms
55,612 KB
testcase_43 AC 158 ms
56,596 KB
testcase_44 AC 155 ms
56,380 KB
testcase_45 AC 321 ms
78,120 KB
testcase_46 AC 138 ms
56,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no332;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
	public static final int MAX = 100000;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long x = sc.nextLong();
		ArrayList<Pair> a = new ArrayList<>(); //i,a
		for(int i=0;i<n;i++) {
			int aa = sc.nextInt();
			a.add(new Pair(i,aa));
		}
		Collections.sort(a,(xx,yy)->Integer.compare(xx.a, yy.a));
		int n2 = 0;
		int max2 = 0;
		for(Pair p:a) {
			if (p.a <= MAX) {
				n2++;
				max2 += p.a;
			}else{
				break;
			}
		}
		boolean[][] dp = new boolean[n2+1][max2+1];
		dp[0][0] = true;
		for(int i=1;i<=n2;i++) {
			int w = a.get(i-1).a;
			for(int j=max2;j>=0;j--) {
				if (j >= w) {
					dp[i][j] |= dp[i-1][j-w];
				}
				dp[i][j] |= dp[i-1][j];
			}
		}
		int n3 = n - n2;
		for(int i=0;i<1<<n3;i++) {
			long sum = 0;
			for(int j=0;j<n3;j++) {
				if ((i>>j&1)>0) {
					sum += a.get(n2 + j).a;
				}
			}
			if (sum > x) {
				continue;
			}
			if (x - sum <= max2 && dp[n2][(int) (x-sum)]) {
				boolean[] ans = new boolean[n];
				for(int j=0;j<n3;j++) {
					if ((i>>j&1)>0) {
						ans[a.get(n2+j).i] = true;
					}
				}
				int now = (int) (x - sum);
				for(int i2=n2;i2>=1;i2--) {
					int w = a.get(i2-1).a;
					if (now >= w && dp[i2-1][now-w]) {
						ans[a.get(i2-1).i] = true;
						now -= w;
					}
				}
				StringBuilder sb = new StringBuilder();
				for(int j=0;j<n;j++) {
					sb.append(ans[j] ? 'o' : 'x');
				}
				System.out.println(sb);
				return;
			}
		}
		System.out.println("No");
	}

}
class Pair {
	int i,a;
	public Pair(int i,int a) {
		this.i = i;
		this.a = a;
	}
}
0