結果

問題 No.626 Randomized 01 Knapsack
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-20 20:48:31
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 3,253 bytes
コンパイル時間 2,454 ms
コンパイル使用メモリ 75,332 KB
実行使用メモリ 54,864 KB
最終ジャッジ日時 2023-08-22 11:00:49
合計ジャッジ時間 5,752 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
    }
}
final class Main {
    long ans=0;
    int p;
    long[]v,w,vAcc,wAcc;
    void dfs(long rem,int a,long sum){
        ans=Math.max(ans,sum);
        if(a>=p)
            return;
        double expect=(double)(vAcc[p]-vAcc[a])*(double)w[a]/(double)v[a];
        if(sum+expect<ans-10)
            return;
        if(wAcc[p]-wAcc[a]<=rem){
            ans=Math.max(ans,vAcc[p]-vAcc[a]+sum);
            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;
        }
    }
}
0