結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
49,668 KB
testcase_01 AC 58 ms
50,564 KB
testcase_02 AC 63 ms
50,920 KB
testcase_03 AC 82 ms
52,176 KB
testcase_04 AC 73 ms
52,120 KB
testcase_05 AC 73 ms
51,700 KB
testcase_06 AC 139 ms
52,256 KB
testcase_07 AC 169 ms
54,036 KB
testcase_08 AC 218 ms
54,460 KB
testcase_09 AC 297 ms
55,320 KB
testcase_10 AC 547 ms
54,976 KB
testcase_11 AC 1,338 ms
55,600 KB
testcase_12 AC 1,577 ms
55,740 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1,194 ms
55,880 KB
testcase_16 AC 1,512 ms
55,484 KB
testcase_17 AC 1,502 ms
55,672 KB
testcase_18 AC 1,468 ms
55,340 KB
testcase_19 AC 1,547 ms
55,380 KB
testcase_20 AC 1,734 ms
55,492 KB
testcase_21 WA -
testcase_22 AC 1,090 ms
55,600 KB
testcase_23 AC 1,500 ms
55,612 KB
testcase_24 AC 133 ms
55,452 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;
        }
        Random rnd=new Random();
        for(int i=0;i<p;++i){
            int j=rnd.nextInt(i+1);
            long t=w[i];
            w[i]=w[j];
            w[j]=t;
            t=v[i];
            v[i]=v[j];
            v[j]=t;
        }
        int U=(int)Math.sqrt(9500000/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);
        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];
                }
            }
        }
        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