Submission #1015896

#TimeUsernameProblemLanguageResultExecution timeMemory
1015896coolsentenceidontrememberKnapsack (NOI18_knapsack)C++17
100 / 100
75 ms35328 KiB
#include<bits/stdc++.h> #pragma GCC optimize("O3", "unroll-loops") #define ll long long #define ld long double #define ff first #define ss second #define db double #define time_begin() auto begin = chrono::high_resolution_clock::now() #define time_end() auto end = chrono::high_resolution_clock::now(); auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end-begin); auto sec = elapsed.count() * 1e-9; cerr << "\n\nExecution time: " << sec << " seconds"; #define check_time() cerr << "\nStatus : "; if (sec>1) cerr << "Time Limit Exceeded 1!!!1!111"; else cerr << "You good brother" using namespace std; void setIO(string s = ""){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); if (!s.empty()){ freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); } } // use two dp array to save space int main(){ setIO(); int s, n; cin >> s >> n; map<int, vector<pair<int, int>>> group; for (int i = 1; i <= n; i++){ int v, w, k; cin >> v >> w >> k; group[w].push_back({v, k}); } for (auto &[key, val] : group) sort(group[key].begin(), group[key].end(), greater<pair<int, int>>()); vector<vector<ll>> dp(group.size()+1, vector<ll>(s+1, -LLONG_MAX)); for (int i = 0; i <= s; i++) dp[0][i] = 0; int cur = 0; for (auto &[weight, items] : group){ cur++; for (int i = 0; i <= s; i++){ dp[cur][i] = dp[cur-1][i]; int num = 0; ll val = 0; int sz = items.size(); int cur_item = 0; int cnt = 0; while (1LL*(num+1)*weight <= i && cur_item < sz && cnt < items[cur_item].ss){ num++; cnt++; val += items[cur_item].ff; if (dp[cur-1][i-num*weight] != -LLONG_MAX) dp[cur][i] = max(dp[cur][i], dp[cur-1][i-num*weight] + val); if (cnt == items[cur_item].ss){ cnt = 0; cur_item++; } } } } cout << *max_element(dp[cur].begin(), dp[cur].end()) << '\n'; }

Compilation message (stderr)

knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:22:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |  freopen((s+".in").c_str(), "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:23:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |  freopen((s+".out").c_str(), "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...