結果
問題 | No.626 Randomized 01 Knapsack |
ユーザー |
![]() |
提出日時 | 2017-12-18 13:36:36 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 966 bytes |
コンパイル時間 | 1,497 ms |
コンパイル使用メモリ | 161,252 KB |
実行使用メモリ | 10,496 KB |
最終ジャッジ日時 | 2024-12-15 23:47:53 |
合計ジャッジ時間 | 38,625 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 13 TLE * 12 |
ソースコード
#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; }