제출 #1126508

#제출 시각아이디문제언어결과실행 시간메모리
1126508mazen_ghanayemKnapsack (NOI18_knapsack)C++20
100 / 100
69 ms22460 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ull unsigned long long #define ld long double #define int long long #define nl "\n" #define oo 1e9 + 1 #define OO 1e18 + 1 #define sp ' ' #define sz(x) (int)(x.size()) #define MOD 1000000007 #define fixed(n) fixed << setprecision(n) #define sub_mod(a, b, m) ((((a) % m) - ((b) % m) + m) % m) #define add_mod(a, b, m) ((((a) % m) + ((b) % m)) % m) #define mult_mod(a, b, m) ((((a) % m) * ((b) % m)) % m) #define EPS 1e-9 #define PI acos(-1) using namespace __gnu_pbds; using namespace std; void fastio() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout); #endif } void solve(int tc) { int s, n; cin >> s >> n; vector<int> w(n * 12 + 5); vector<int> v(n * 12 + 5); map<int, vector<pair<int, int>>> mp; while (n--) { int weight, value, q; cin >> value >> weight >> q; mp[weight].push_back({value, q}); } int cnt = 1; for (auto [weight, vec] : mp) { sort(vec.rbegin(), vec.rend()); int tot = 0; for (auto [value, q] : vec){ int x = s / weight; q = min(q, x + 1); int k = 1; tot += q * weight; while (k <= q) { w[cnt] = weight * k; v[cnt] = value * k; q -= k; k *= 2; cnt++; } if (q > 0) { w[cnt] = weight * q; v[cnt] = value * q; cnt++; } if(tot >= s) break; } } n = cnt; vector<int> dp(s + 5); for (int i = 1; i <= n; i++) { vector<int> ndp = dp; for (int ww = w[i]; ww <= s; ww++) { ndp[ww] = max(ndp[ww], dp[ww - w[i]] + v[i]); } dp = ndp; } cout << dp[s] << nl; } signed main(void) { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); //fastio(); int tc = 1; // cin >> tc; int i = 1; while (tc--) { // cout<<"Case #"<<i<<": "; solve(i++); } return 0; }

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'void fastio()':
knapsack.cpp:25:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:25:46: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
      |                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...