結果

問題 No.626 Randomized 01 Knapsack
ユーザー たこしたこし
提出日時 2017-12-18 13:36:36
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 966 bytes
コンパイル時間 1,879 ms
コンパイル使用メモリ 147,164 KB
実行使用メモリ 8,752 KB
最終ジャッジ日時 2023-08-22 05:25:12
合計ジャッジ時間 5,415 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using LL = long long int;

const int MAX_N = 50005;

int N;
class Gift{
public:
  LL V, W;
  double value;
  void input()
  {
    cin >> V >> W;
    value = (double)V/(double)W;
  }

  bool operator < (const Gift& p) const {
    return value < p.value;
  }

  bool operator > (const Gift& p) const {
    return value > p.value;
  }
}G[MAX_N];
LL W;

LL ans = 0;
LL solve(int pos = 0, LL tmpV = 0, LL tmpW = 0)
{
  if((double)ans > tmpV + (double)(W-tmpW)*G[pos].value) {
    return tmpV;
  }

  if(pos == N) {
    ans = max(ans, tmpV);
    return tmpV;
  }

  LL ret = 0;
  if(tmpW + G[pos].W <= W) {
    ret = max(ret, solve(pos+1, tmpV+G[pos].V, tmpW+G[pos].W));
  }
  ret = max(ret, solve(pos+1, tmpV, tmpW));
  ans = max(ret, ans);
  return ret;
}

int main()
{
  cin >> N >> W;
  for(int i = 0; i < N; i++) {
    G[i].input();
  }

  sort(G, G+N, greater<Gift>());
  solve();
  cout << ans << endl;

  return 0;
}
0