이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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<int>[s + 1];
for (int i = 0; i <= s; i++)
{
e[i] = vector<int>();
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];
for (int i = 1; i <= s; i++)
{
if (e[i].size() < 1)
{
continue;
}
for (int j = 0; j <= s; j++)
{
h[j] = 0;
}
for (int j = 1; j <= s; j++)
{
if (j - i < 0)
{
continue;
}
if (dp[j - i] == -1)
{
continue;
}
if (e[i].size() <= h[j - i])
{
// nigdy nie powinien być mniejszy, co najwyżej większy lub równy
continue;
}
if (dp[j] == -1)
{
dp[j] = dp[j - i] + e[i][h[j - i]];
h[j] = h[j - i] + 1;
continue;
}
}
for (int j = 1; j <= s; j++)
{
if (j - i < 0)
{
continue;
}
if (dp[j - i] == -1)
{
continue;
}
if (e[i].size() <= h[j - i])
{
// nigdy nie powinien być mniejszy, co najwyżej większy lub równy
continue;
}
if (dp[j] == -1)
{
dp[j] = dp[j - i] + e[i][h[j - i]];
h[j] = h[j - i] + 1;
continue;
}
if (dp[j] < dp[j - i] + e[i][h[j - i]])
{
dp[j] = dp[j - i] + e[i][h[j - i]];
h[j] = h[j - i] + 1;
}
}
}
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];
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:27:39: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
27 | while (!q[i].empty() && e[i].size() < s)
| ~~~~~~~~~~~~^~~
knapsack.cpp:80:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
80 | if (e[i].size() <= h[j - i])
| ~~~~~~~~~~~~^~~~~~~~~~~
knapsack.cpp:102:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
102 | if (e[i].size() <= h[j - i])
| ~~~~~~~~~~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |