結果
問題 | No.332 数列をプレゼントに |
ユーザー |
![]() |
提出日時 | 2015-12-25 01:34:27 |
言語 | Java (openjdk 23) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,691 bytes |
コンパイル時間 | 2,981 ms |
コンパイル使用メモリ | 93,776 KB |
実行使用メモリ | 48,336 KB |
最終ジャッジ日時 | 2024-12-24 07:29:22 |
合計ジャッジ時間 | 15,525 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 41 TLE * 1 |
ソースコード
package no332;import java.util.ArrayList;import java.util.Collections;import java.util.Scanner;public class Main {public static final int MAX = 10000;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,afor(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;}}