제출 #1096246

#제출 시각아이디문제언어결과실행 시간메모리
1096246InvMODKnapsack (NOI18_knapsack)C++14
100 / 100
46 ms4440 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+1; const int maxN = 1e5+1; int S, n, w[maxN], v[maxN], k[maxN]; 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]; 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, 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"; } int32_t 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; }

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

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