이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
int n, m, k, p, q, u, v, w, l, r, x, y, z;
// #define int long long
const int N = 1e3 + 10, inf = 1e9, mod = 998244353;
mt19937_64 randll(chrono::steady_clock::now().time_since_epoch().count());
vector<vector<char>> dp;
void solve (int tc = 1) {
scanf("%d%d", &w, &n);
vector<int> dp(w + 1, -inf);
dp[0] = 0;
for (int _ = 0; _ < n; _++) {
int val, wt, freq;
scanf("%d%d%d", &val, &wt, &freq);
freq = min(freq, w);
for (int md = 0; md < wt; md++) {
deque<pair<int, int>> q;
for (int i = md; i <= w; i += wt) {
while (!q.empty() and q.front().second < i - wt * freq) q.pop_front();
int oval = dp[i];
if (!q.empty()) {
dp[i] = max(dp[i], (i - q.front().second) / wt * val + q.front().first);
}
while (!q.empty() and q.back().first + (i - q.back().second) / wt * val < oval) q.pop_back();
q.push_back(make_pair(oval, i));
}
}
}
printf("%d", *max_element(dp.begin(), dp.end()));
}
#undef int
int main() {
// ios_base::sync_with_stdio(0); cin.tie(0);
int tc = 1;
// cin >> tc;
// scanf("%d", &tc);
while (tc--) {
solve(tc);
// if (tc) cout << nl;
// if (tc) printf("\n");
}
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve(int)':
knapsack.cpp:12:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%d%d", &w, &n);
| ~~~~~^~~~~~~~~~~~~~~~
knapsack.cpp:17:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%d%d%d", &val, &wt, &freq);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |