Submission #1282221

#TimeUsernameProblemLanguageResultExecution timeMemory
1282221kurryktKnapsack (NOI18_knapsack)C++20
100 / 100
157 ms11532 KiB
#include <bits/stdc++.h> using namespace std; using pii = pair <long long, long long>; const int N = 1e6 + 20; long long n, S, v[N], w[N], t[N]; vector <pii> col[N]; struct Data{ long long w, v, t; }; vector <Data> V; long long dp[N]; void Process(){ cin >> S >> n; for (int i = 1; i <= n; i++){ cin >> v[i] >> w[i] >> t[i]; t[i] = min(t[i] , S / w[i]); if (w[i] > S) continue; col[w[i]].push_back({v[i], t[i]}); } for (int i = 1; i <= S; i++){ sort(col[i].begin(), col[i].end(), greater<pii>()); long long t = 0, id = 0; for (pii x : col[i]){ id++; t += x.second; if (t > S) break; } id++; while (col[i].size() >= id) col[i].pop_back(); } for (int i = 1; i <= S; i++){ for (pii x : col[i]){ long long val = x.first, T = x.second; int LG = __lg(T); // cout << i << " " << val << " " << T <<'\n'; for (int k = 0; k <= LG; k++){ if (T < (1LL << k)) break; V.push_back({i, val * (1LL << k), (1LL << k)}); T -= (1LL << k); } if (T != 0) V.push_back({i, T * val, T}); } } for (Data x : V){ int w = x.w, v = x.v, t = x.t; // cout << w << " " << v << " " << t <<'\n'; for (int j = S; j >= w * t; j--){ dp[j] = max(dp[j], dp[j - w * t] + v); } } cout << dp[S]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define TASK "test" if (fopen(TASK".inp","r")){ freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout); } Process(); return 0; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         freopen(TASK".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen(TASK".out","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...