結果

問題 No.626 Randomized 01 Knapsack
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-15 21:27:12
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,608 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 74,464 KB
実行使用メモリ 56,224 KB
最終ジャッジ日時 2023-08-21 23:11:41
合計ジャッジ時間 25,870 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,484 KB
testcase_01 AC 53 ms
50,924 KB
testcase_02 AC 67 ms
52,072 KB
testcase_03 AC 63 ms
51,084 KB
testcase_04 AC 73 ms
52,048 KB
testcase_05 AC 68 ms
51,768 KB
testcase_06 AC 158 ms
52,576 KB
testcase_07 AC 188 ms
54,312 KB
testcase_08 AC 268 ms
54,504 KB
testcase_09 AC 347 ms
55,008 KB
testcase_10 AC 656 ms
55,460 KB
testcase_11 AC 1,828 ms
56,108 KB
testcase_12 AC 1,989 ms
55,892 KB
testcase_13 WA -
testcase_14 AC 632 ms
56,224 KB
testcase_15 WA -
testcase_16 TLE -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 TLE -
testcase_21 WA -
testcase_22 AC 1,473 ms
56,092 KB
testcase_23 TLE -
testcase_24 AC 132 ms
55,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    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;
        }
        int U=(int)Math.sqrt(10500000/p);
        int W=p*U+1;
        long D=Math.max(1,ww/(W-1));
        int[]vt=new int[p];
        for(int i=0;i<p;++i)
            vt[i]=(int)((w[i]+D-1)/D);
        long[]dp=new long[W];
        long[]dp2=new long[W];
        Arrays.fill(dp,0);
        for(int i=0;i<p;++i){
            for(int j=W-1;j>=vt[i];--j){
                if(dp[j]<dp[j-vt[i]]+v[i]||
                   (dp[j]==dp[j-vt[i]]+v[i]
                    &&dp2[j]>dp2[j-vt[i]]+w[i])){
                    dp[j]=dp[j-vt[i]]+v[i];
                    dp2[j]=dp2[j-vt[i]]+w[i];
                }
            }
        }
        long ans=0;
        for(int i=0;i<W;++i)
            if(dp2[i]<=ww)
                ans=Math.max(ans,dp[i]);
        out.println(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