結果
問題 |
No.332 数列をプレゼントに
|
ユーザー |
![]() |
提出日時 | 2015-12-25 01:04:50 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,010 bytes |
コンパイル時間 | 2,658 ms |
コンパイル使用メモリ | 91,584 KB |
実行使用メモリ | 229,664 KB |
最終ジャッジ日時 | 2024-09-18 23:27:52 |
合計ジャッジ時間 | 18,819 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 25 WA * 16 TLE * 1 |
ソースコード
package no332; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { 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 ones = 0; for(Pair p:a) { if (p.a == 1) { ones++; }else{ break; } } int n2 = n - ones; int n3 = n2 / 2; int n4 = n2 - n3; ArrayList<Pair2> al = new ArrayList<>(); for(int i=0;i<1<<n3;i++) { long sum = 0; for(int j=0;j<n3;j++) { if ((i>>j&1)==1) { sum += a.get(ones + j).a; } } al.add(new Pair2(i, sum)); } Collections.sort(al,(xx,yy)->Long.compare(xx.sum, yy.sum)); for(int i=0;i<1<<n4;i++) { long sum = 0; for(int j=0;j<n4;j++) { if ((i>>j&1)==1) { sum += a.get(ones + n3 + j).a; } } if (sum > x) { continue; } long key = x - sum; int l = 0; int r = al.size(); while(l + 1 < r) { int c = (l + r) >>> 1; if (al.get(c).sum <= key) { l = c; }else{ r = c; } } if (sum + al.get(l).sum + ones >= x) { boolean[] ans = new boolean[n]; for(int j=0;j<n3;j++) { if ((al.get(l).mask >> j & 1) == 1) { ans[a.get(ones + j).i] = true; } } for(int j=0;j<n4;j++) { if ((i>>j&1)==1) { ans[a.get(ones + n3 + j).i] = true; } } for(int j=0;j<x-sum-al.get(l).sum;j++) { ans[j] = true; } 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; } } class Pair2 { int mask; long sum; public Pair2(int mask,long sum) { this.mask = mask; this.sum = sum; } }