| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1321486 | NgTrung2217 | Knapsack (NOI18_knapsack) | C++20 | 40 ms | 5340 KiB |
#include <bits/stdc++.h>
#define task ""
#define ff first
#define ss second
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pll = pair <ll, ll>;
const char el = '\n';
const char sp = ' ';
const int maxS = 2005;
const int maxN = 1e5 + 5;
ll s, n;
ll v[maxN], w[maxN], k[maxN];
ll dp[maxS];
void solve ()
{
if (!(cin >> s >> n)) return;
for (int i = 1;i <= n;++i)
cin >> v[i] >> w[i] >> k[i];
ll max_k = 0;
for (int i = 1;i <= n;++i)
max_k = max(max_k, k[i]);
if (n == 1)
{
cout << v[1] * min(s / w[1], k[1]) << el;
}
else if (n <= 100 && max_k <= 10)
{
for (int i = 0;i <= s;++i) dp[i] = 0;
for (int i = 1;i <= n;++i)
{
for (int j = s;j >= w[i];--j)
{
for (int t = 0;t <= k[i];++t)
{
if (j >= w[i] * t)
{
dp[j] = max(dp[j], dp[j - w[i] * t] + v[i] * t);
}
}
}
}
cout << dp[s] << el;
}
else
{
vector <pll> obj[maxS];
for (int i = 0;i <= s;++i) dp[i] = 0;
for (int i = 1;i <= n;++i)
{
if (w[i] <= s) obj[w[i]].push_back({v[i], k[i]});
}
for (int i = 1;i <= s;++i)
{
if (obj[i].empty()) continue;
sort(obj[i].begin(), obj[i].end(), greater <pll>());
int id = 0;
for (int count = 0; count * i < s; ++count)
{
if (id >= (int)obj[i].size()) break;
for (int j = s; j >= i; --j)
{
dp[j] = max(dp[j], dp[j - i] + obj[i][id].ff);
}
obj[i][id].ss--;
if (obj[i][id].ss == 0) id++;
}
}
cout << dp[s] << el;
}
}
int main ()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
