이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <vector>
#define int long long
using namespace std;
const int maxn = 1e5 + 10, maxs = 2e3 + 10, maxlog = 12;
int n, s;
struct item { int w, v, quantity; } items[maxn];
vector < pair < int, int > > by_w[maxs];
vector < pair < int, int > > sequence;
int dp[2][maxs];
void solve() {
sequence.push_back({0, 0});
for (int i = 1 ; i <= s ; ++i) {
if (by_w[i].empty()) continue;
sort(by_w[i].begin(), by_w[i].end(), greater < pair < int, int > > ());
int cnt = 0, j = 0, currcnt;
while (j < by_w[i].size() && cnt < s/i) {
currcnt = 0;
while (cnt < s/i && currcnt < by_w[i][j].second) {
sequence.push_back({i, by_w[i][j].first});
++cnt;
++currcnt;
}
++j;
}
}
for (int ix = sequence.size()-1 ; ix >= 1 ; --ix)
for (int taken = 0 ; taken <= s ; ++taken) {
dp[ix&1][taken] = dp[(ix+1)&1][taken];
if (taken + sequence[ix].first <= s) dp[ix&1][taken] = max(dp[ix&1][taken], dp[(ix+1)&1][taken + sequence[ix].first] + sequence[ix].second);
}
cout << dp[1][0] << '\n';
}
void fast_io() {
ios_base :: sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
}
void read() {
cin >> s >> n;
for (int i = 1 ; i <= n ; ++i) {
cin >> items[i].v >> items[i].w >> items[i].quantity;
if (items[i].w <= s) by_w[items[i].w].push_back({items[i].v, items[i].quantity});
}
}
signed main () {
// freopen("feast.in", "r", stdin);
// freopen("feast.out", "w", stdout);
fast_io();
read();
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:24:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | while (j < by_w[i].size() && cnt < s/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... |