結果
| 問題 |
No.626 Randomized 01 Knapsack
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-12-22 04:02:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,473 bytes |
| コンパイル時間 | 2,348 ms |
| コンパイル使用メモリ | 199,148 KB |
| 最終ジャッジ日時 | 2025-01-05 06:13:11 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 8 WA * 1 TLE * 16 |
ソースコード
#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
os << "sz:" << v.size() << "\n[";
for (const auto& p : v) {
os << p << ",";
}
os << "]\n";
return os;
}
template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
os << "(" << p.first << "," << p.second
<< ")";
return os;
}
struct Product {
ll v;
ll w;
bool operator<(const Product& p) const
{
return v * p.w < w * p.v;
}
bool operator>(const Product& p) const
{
return v * p.w > w * p.v;
}
};
ostream& operator<<(ostream& os, const Product& p)
{
os << "<" << p.v << ", " << p.w << ">";
return os;
}
ll ans = 0;
int N;
ll W;
void dfs(const vector<Product>& product, const int ind, const ll weight, const ll value)
{
if (ind == N) {
ans = max(ans, value);
return;
}
if (weight + product[ind].w <= W) {
ll relaxed = value + product[ind].v;
ll use = weight + product[ind].w;
for (int i = ind + 1; i < N; i++) {
if (use + product[i].w <= W) {
use += product[i].w;
relaxed += product[i].v;
} else {
use = W;
relaxed += (product[i].v * (W - use)) / product[i].w;
break;
}
}
if (relaxed + value > ans) {
dfs(product, ind + 1, weight + product[ind].w, value + product[ind].v);
}
}
ll relaxed = value;
ll use = weight;
for (int i = ind + 1; i < N; i++) {
if (use + product[i].w <= W) {
use += product[i].w;
relaxed += product[i].v;
} else {
use = W;
relaxed += (product[i].v * (W - use)) / product[i].w;
break;
}
}
if (relaxed + value > ans) {
dfs(product, ind + 1, weight, value);
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> W;
vector<Product> product;
for (int i = 0; i < N; i++) {
ll v, w;
cin >> v >> w;
if (w > W) {
continue;
}
product.push_back(Product{v, w});
}
N = product.size();
sort(product.begin(), product.end(), greater<Product>{});
dfs(product, 0, 0, 0);
cout << ans << endl;
return 0;
}