結果

問題 No.626 Randomized 01 Knapsack
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-15 21:22:52
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,488 bytes
コンパイル時間 3,509 ms
コンパイル使用メモリ 74,576 KB
実行使用メモリ 57,932 KB
最終ジャッジ日時 2023-08-21 19:13:00
合計ジャッジ時間 22,640 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,464 KB
testcase_01 AC 46 ms
49,956 KB
testcase_02 AC 50 ms
49,544 KB
testcase_03 AC 57 ms
50,560 KB
testcase_04 AC 59 ms
50,616 KB
testcase_05 AC 61 ms
50,996 KB
testcase_06 WA -
testcase_07 AC 120 ms
52,020 KB
testcase_08 AC 157 ms
52,448 KB
testcase_09 AC 249 ms
55,324 KB
testcase_10 AC 425 ms
54,796 KB
testcase_11 AC 1,550 ms
57,860 KB
testcase_12 AC 1,812 ms
57,700 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1,746 ms
57,704 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1,964 ms
57,904 KB
testcase_21 WA -
testcase_22 AC 1,245 ms
57,384 KB
testcase_23 AC 1,746 ms
57,732 KB
testcase_24 AC 134 ms
57,372 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 U=50;
        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;
        }
        long D=Math.max(1,ww/U/p);
        int[]vt=new int[p];
        for(int i=0;i<p;++i)
            vt[i]=(int)((w[i]+D-1)/D);
        int W=p*U+1;
        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