Submission #1175318

#TimeUsernameProblemLanguageResultExecution timeMemory
1175318pete555Knapsack (NOI18_knapsack)C++17
100 / 100
36 ms1984 KiB
#include<bits/stdc++.h> using namespace std; #define pi pair<int,int> #define ll long long #define pb push_back #define pf push_front void fileIO(string filename) { freopen((filename + ".in").c_str(), "r", stdin); freopen((filename + ".out").c_str(), "w", stdout); } const int MOD = 1e9+7; int main() { cin.tie(0)->sync_with_stdio(false); //fileIO(""); int S, n; cin >> S >> n; unordered_map<int,vector<pi>> m; for(int i=0; i<n; i++){ int v, w, k; cin >> v >> w >> k; m[w].pb({v, k}); } vector<int> dp(S+1); for(auto I: m){ // 1 <= w <= S sort(I.second.begin(), I.second.end(), greater<pi>()); int w = I.first; for(int i=S; i>=0; i--){ int cnt = 0, idx = 0, used = 0, profit = 0; while((cnt+1)*w <= i and idx < I.second.size()){ cnt++, used++; profit += I.second[idx].first; dp[i] = max(dp[i], dp[i-cnt*w] + profit); if(used == I.second[idx].second){ used = 0; idx++; } } } } cout << dp[S] << '\n'; }

Compilation message (stderr)

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