Submission #1090111

#TimeUsernameProblemLanguageResultExecution timeMemory
1090111StaheosKnapsack (NOI18_knapsack)C++14
100 / 100
26 ms1836 KiB
#include <iostream> #include <vector> #include <queue> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 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 && e[i].size() * i < s) //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; long long* dp = new long long[s + 1]; for (int i = 1; i <= s; i++) { dp[i] = -1; } dp[0] = 0; 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++) //for (int k = 0; k < e[i].size() && j + i * (k + 1) <= s && i * k <= 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]; } else { break; } } } } 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:31:39: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   31 |   while (!q[i].empty() && e[i].size() < s && e[i].size() * i < s)
      |                           ~~~~~~~~~~~~^~~
knapsack.cpp:31:62: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   31 |   while (!q[i].empty() && e[i].size() < s && e[i].size() * i < s)
      |                                              ~~~~~~~~~~~~~~~~^~~
knapsack.cpp:66:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |    for (int k = 0; k < e[i].size() && j + i * (k + 1) <= s; k++)
      |                    ~~^~~~~~~~~~~~~
#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...