Submission #1090092

#TimeUsernameProblemLanguageResultExecution timeMemory
1090092StaheosKnapsack (NOI18_knapsack)C++14
100 / 100
202 ms35724 KiB
#include <iostream> #include <vector> #include <queue> using namespace std; int main() { int n, s; cin >> s >> n; auto q = new priority_queue<pair<int, int>>[s + 1]; for (int i = 0; i <= s; i++) { q[i] = priority_queue<pair<int, int>>(); } for (int i = 0; i < n; i++) { int v, w, k; cin >> v >> w >> k; q[w].push(pair<int, int>(v, k)); } auto e = new vector<long long>[s + 1]; for (int i = 0; i <= s; i++) { e[i] = vector<long long>(); while (!q[i].empty() && e[i].size() < s) { auto p = q[i].top(); q[i].pop(); e[i].push_back(p.first); p.second--; if (p.second > 0) { q[i].push(p); } } } delete[] q; /*for (int i = 0; i <= s; i++) { cout << "i: " << i << ' '; for (int j = 0; j < e[i].size(); j++) { cout << e[i][j] << ' '; } cout << '\n'; }*/ long long* dp = new long long[s + 1]; for (int i = 1; i <= s; i++) { dp[i] = -1; } dp[0] = 0; int* h = new int[s + 1]; long long* prev = new long long[s + 1]; for (int i = 1; i <= s; i++) { if (e[i].size() < 1) { continue; } for (int j = s; j >= 0; j--) { if (dp[j] == -1) { continue; } for (int k = 0; k < e[i].size() && j + i * (k + 1) <= s; k++) { if (dp[j + i * (k + 1)] == -1) { dp[j + i * (k + 1)] = dp[j + i * k] + e[i][k]; } else if (dp[j + i * (k + 1)] < dp[j + i * k] + e[i][k]) { dp[j + i * (k + 1)] = dp[j + i * k] + e[i][k]; } } } } int m = 0; for (int i = 0; i <= s; i++) { //cout << "dp " << i << ' ' << dp[i] << '\n'; if (dp[i] > dp[m]) { m = i; } } cout << dp[m]; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:27:39: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   27 |   while (!q[i].empty() && e[i].size() < s)
      |                           ~~~~~~~~~~~~^~~
knapsack.cpp:73:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |    for (int k = 0; k < e[i].size() && j + i * (k + 1) <= s; k++)
      |                    ~~^~~~~~~~~~~~~
knapsack.cpp:59:7: warning: unused variable 'h' [-Wunused-variable]
   59 |  int* h = new int[s + 1];
      |       ^
knapsack.cpp:60:13: warning: unused variable 'prev' [-Wunused-variable]
   60 |  long long* prev = new long long[s + 1];
      |             ^~~~
#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...