Submission #1096239

#TimeUsernameProblemLanguageResultExecution timeMemory
1096239InvMODKnapsack (NOI18_knapsack)C++14
73 / 100
1 ms520 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;} template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;} const int N = 2e3+5; int S, n, w[N], v[N], k[N]; ll dp[N], answer; vector<int> bag[N]; void process(int cur_w, ll cur_v){ for(int i = S; i >= cur_w; i--){ ckmx(dp[i], dp[i-cur_w] + cur_v); ckmx(answer, dp[i]); } return; } void solve() { cin >> S >> n; for(int i = 1; i <= n; i++){ cin >> v[i] >> w[i] >> k[i]; if(w[i] <= S){ bag[w[i]].push_back(i); } } for(int cur_w = 1; cur_w <= S; cur_w++){ sort(bag[cur_w].begin(), bag[cur_w].end(), [&](int x, int y){ return v[x] > v[y]; }); int need = S / cur_w+1, pointer = 0; while(pointer < (int) bag[cur_w].size() && need){ ll cur_v = v[bag[cur_w][pointer]]; int cnt = k[bag[cur_w][pointer]]; int can = min(need, cnt); need = need - can; for(int j = 0; j < (32 - __builtin_clz(can)); j++){ if(can >= (1<<j)){ can -= (1<<j); process(cur_w * (1<<j), cur_v * (1ll<<j)); } else break; } if(can){ process(cur_w * can, cur_v * (1ll * can)); } pointer++; } } cout << answer <<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "task" if(fopen(name".INP", "r")){ freopen(name".INP", "r", stdin); freopen(name".OUT", "w", stdout); } int t = 1; //cin >> t; while(t--){ solve(); } return 0; }

Compilation message (stderr)

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