이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define ll long long
#define yn(tf) cout << (tf ? "YES\n" : "NO\n")
#define pr(v) v.begin(), v.end()
#define Endl endl
#define ld long double
const ld PI = 3.14159265359;
const ll MOD = 998244353;
const ll N = 1e5 + 7;
int s, n;
vector<vector<ll>> v;
vector<vector<ll>> mem;
ll fun(int i, int rem) {
if (rem < 0)
return -1e15;
if (i == v.size() || rem == 0)
return 0;
ll &res = mem[i][rem];
if (res != -1)
return res;
res = fun(i + 1, rem);
for (ll j = 1; j <= min(rem / v[i][1] + 3, v[i][2]); j++)
res = max(res, fun(i + 1, rem - j * v[i][1]) + v[i][0] * j);
return res;
}
int main() {
FAST
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// int V[N], W[N], K[N];
cin >> s >> n;
vector<vector<pair<int, int>>> mp(2007);
for (int i = 0; i < n; i++) {
int V,W,K;
cin >> V >> W >> K;
mp[W].push_back({V, K});
}
for (int i = 1; i <= 2000; i++) {
vector<pair<int, int>> &tmp = mp[i];
int wei = i;
sort(pr(tmp));
reverse(pr(tmp));
int sum = 0;
int mx=(s+i-1)/i;
for (int i = 0; i < tmp.size()&&sum<mx; i++) {
if (i > 0) {
if (v.back()[0] == tmp[i].first) {
v.back()[2] += max(0, min(tmp[i].second, mx - sum));
sum += tmp[i].second;
} else {
v.push_back(
{tmp[i].first, wei, max(0, min(tmp[i].second, mx - sum))});
sum += tmp[i].second;
}
} else {
v.push_back(
{tmp[i].first, wei, max(0, min(tmp[i].second, mx - sum))});
sum += tmp[i].second;
}
}
mp[i].clear();
}
mp.clear();
mem = vector<vector<ll>>(v.size() + 1, vector<ll>(s + 1));
for (int i = 0; i <= v.size(); i++)
for (int j = 0; j <= s; j++)
mem[i][j] = -1;
cout << fun(0, s);
return 0;
}
/*
*/
// BEFORE coding are you sure you understood the statement correctly?
// PLEASE do not forget to read the sample explanation carefully.
// WATCH out for overflows & RTs in general.
// TEST your idea or code on the corner cases.
// ANALYZE each idea you have thoroughly.
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'long long int fun(int, int)':
knapsack.cpp:26:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | if (i == v.size() || rem == 0)
| ~~^~~~~~~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < tmp.size()&&sum<mx; i++) {
| ~~^~~~~~~~~~~~
knapsack.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int i = 0; i <= v.size(); i++)
| ~~^~~~~~~~~~~
# | 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... |