Submission #733513

#TimeUsernameProblemLanguageResultExecution timeMemory
733513chanhchuong123Knapsack (NOI18_knapsack)C++14
100 / 100
59 ms3892 KiB
#include <bits/stdc++.h> using namespace std; #define task "" #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i) template <typename T1, typename T2> bool minimize(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maximize(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } const int dx[] = {-1, 0, 0, +1}; const int dy[] = {0, -1, +1, 0}; const int MAX = 2020; int S, N, dp[MAX]; vector<pair<int, int>> a; vector<pair<int, int>> v[MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } cin >> S >> N; for (int i = 1; i <= N; ++i) { int V, W, K; cin >> V >> W >> K; v[W].push_back({V, K}); } for (int i = 1; i <= 2000; ++i) { sort(all(v[i])); int maxPro = S / i; while (maxPro > 0 && v[i].size()) { a.push_back({v[i].back().fi, i}); --maxPro; if (!(--v[i].back().se)) v[i].pop_back(); } } for (pair<int, int> &x: a) { for (int j = S; j >= x.se; --j) maximize(dp[j], dp[j - x.se] + x.fi); } cout << *max_element(dp, dp + S + 1); return 0; }

Compilation message (stderr)

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