結果
問題 |
No.332 数列をプレゼントに
|
ユーザー |
![]() |
提出日時 | 2015-12-25 01:21:38 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,656 bytes |
コンパイル時間 | 2,771 ms |
コンパイル使用メモリ | 84,808 KB |
実行使用メモリ | 52,856 KB |
最終ジャッジ日時 | 2024-09-18 23:32:54 |
合計ジャッジ時間 | 14,877 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 39 WA * 3 |
ソースコード
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; for(Pair p:a) { if (p.a <= MAX) { n2++; }else{ break; } } boolean[][] dp = new boolean[n2+1][MAX+1]; dp[0][0] = true; for(int i=1;i<=n2;i++) { int w = a.get(i-1).a; for(int j=MAX;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 <= MAX && 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; } }