結果

問題 No.626 Randomized 01 Knapsack
ユーザー TsReaperTsReaper
提出日時 2018-03-26 19:14:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 1,028 bytes
コンパイル時間 457 ms
コンパイル使用メモリ 43,428 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 10:14:09
合計ジャッジ時間 2,479 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 9 ms
4,376 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 18 ms
4,376 KB
testcase_12 AC 105 ms
4,376 KB
testcase_13 AC 31 ms
4,384 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 75 ms
4,380 KB
testcase_16 AC 61 ms
4,380 KB
testcase_17 AC 61 ms
4,380 KB
testcase_18 AC 61 ms
4,376 KB
testcase_19 AC 32 ms
4,380 KB
testcase_20 AC 132 ms
4,380 KB
testcase_21 AC 45 ms
4,384 KB
testcase_22 AC 154 ms
4,380 KB
testcase_23 AC 171 ms
4,376 KB
testcase_24 AC 17 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
#define V first
#define W second
using namespace std;
typedef pair<long long,long long> pll;

int n;
long long m,ans;
pll item[5010];

bool flag[5010];

bool cmp(pll a,pll b)
{
    return 1.0*a.V/a.W > 1.0*b.V/b.W;
}

void dfs(long long v,long long w)
{
    int i;
    long long nv = v,nw = w;

    for(i=1;i<=n;i++) if(!flag[i])
    {
        if(nw + item[i].W <= m)
        {
            nv += item[i].V, nw += item[i].W;
            if(nw == m) { ans = max(ans,nv); return; }
        }
        else
        {
            if((long long)(nv + 1.0*(m-nw)/item[i].W*item[i].V) <= ans) return;
            break;
        }
    }
    ans = max(ans,nv);
    if(i > n) return;

    flag[i] = true;
    dfs(v,w);
    if(w + item[i].W <= m) dfs(v+item[i].V,w+item[i].W);
    flag[i] = false;
}

int main()
{
    int i;

    scanf("%d%lld",&n,&m);
    for(i=1;i<=n;i++) scanf("%lld%lld",&item[i].V,&item[i].W);
    sort(item+1,item+n+1,cmp);

    dfs(0,0);
    printf("%lld",ans);
    return 0;
}
0