結果
問題 | No.626 Randomized 01 Knapsack |
ユーザー | 夕叢霧香(ゆうむらきりか) |
提出日時 | 2017-12-20 21:14:32 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 178 ms / 2,000 ms |
コード長 | 3,569 bytes |
コンパイル時間 | 3,404 ms |
コンパイル使用メモリ | 85,368 KB |
実行使用メモリ | 55,936 KB |
最終ジャッジ日時 | 2024-06-26 03:30:34 |
合計ジャッジ時間 | 7,266 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 59 ms
50,720 KB |
testcase_01 | AC | 58 ms
50,324 KB |
testcase_02 | AC | 60 ms
50,260 KB |
testcase_03 | AC | 63 ms
50,584 KB |
testcase_04 | AC | 63 ms
50,072 KB |
testcase_05 | AC | 76 ms
50,508 KB |
testcase_06 | AC | 89 ms
51,628 KB |
testcase_07 | AC | 102 ms
51,748 KB |
testcase_08 | AC | 109 ms
51,920 KB |
testcase_09 | AC | 142 ms
53,160 KB |
testcase_10 | AC | 138 ms
52,964 KB |
testcase_11 | AC | 165 ms
54,216 KB |
testcase_12 | AC | 171 ms
53,776 KB |
testcase_13 | AC | 163 ms
55,544 KB |
testcase_14 | AC | 162 ms
55,936 KB |
testcase_15 | AC | 165 ms
53,952 KB |
testcase_16 | AC | 175 ms
54,016 KB |
testcase_17 | AC | 178 ms
53,892 KB |
testcase_18 | AC | 173 ms
53,948 KB |
testcase_19 | AC | 171 ms
54,004 KB |
testcase_20 | AC | 165 ms
54,180 KB |
testcase_21 | AC | 156 ms
54,128 KB |
testcase_22 | AC | 162 ms
55,892 KB |
testcase_23 | AC | 165 ms
55,236 KB |
testcase_24 | AC | 160 ms
55,260 KB |
ソースコード
import java.io.*; import java.util.*; final class P implements Comparable<P>{ double a; long b; P(double x,long y){a=x;b=y;} @Override public int compareTo(P o){ return Double.compare(a,o.a); } } class Main { long ans=0; int p; long[]v,w,vAcc,wAcc; /*解説を参考にした。*/ final long relaxed(long rem,int a){ long ans=0; for(int i=a;i<p;++i){ if(rem>=w[i]){ rem-=w[i]; ans+=v[i]; }else{ ans+=v[i]*rem/w[i]; rem=0; break; } } return ans; } final void dfs(long rem,int a,long sum){ ans=Math.max(ans,sum); if(a>=p) return; if(wAcc[p]-wAcc[a]<=rem){ ans=Math.max(ans,vAcc[p]-vAcc[a]+sum); return; } long expect=relaxed(rem,a); if(sum+expect<ans) return; if(rem>=w[a]) dfs(rem-w[a],a+1,sum+v[a]); dfs(rem,a+1,sum); } Main(long[]v,long[]w,long ww, P[]rat){ p=rat.length; this.v=new long[p]; this.w=new long[p]; for(int i=0;i<p;++i){ int ind=(int)rat[i].b; this.v[i]=v[ind]; this.w[i]=w[ind]; } this.vAcc=new long[p+1]; this.wAcc=new long[p+1]; for(int i=0;i<p;++i){ this.vAcc[i+1]=this.vAcc[i]+this.v[i]; this.wAcc[i+1]=this.wAcc[i]+this.w[i]; } dfs(ww,0,0); } public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); int n=sc.nextInt(); long ww=sc.nextLong(); int p=0; long[]v=new long[n],w=new long[n]; for(int i=0;i<n;++i){ v[p]=sc.nextInt(); w[p]=sc.nextInt(); if(w[i]>ww)continue; p++; } if (p==0){ out.println(0); out.close(); return; } P[]rat=new P[p]; for(int i=0;i<p;++i) rat[i]=new P((double)v[i]/(double)w[i],i); Arrays.sort(rat,Collections.reverseOrder()); Main main=new Main(v,w,ww,rat); out.println(main.ans); out.close(); } // http://codeforces.com/blog/entry/7018 //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }