이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct chiso{
int val, weight, quantity;
};
chiso a[100005];
vector <int> D[2005];
long long dp[2005][2005];
bool cmp(int x, int y){
return a[x].val > a[y].val;
}
int main(){
//freopen("a.inp", "r", stdin); freopen("a.out", "w", stdout);
int s, n; cin >> s >> n;
for (int i = 1; i <= n; i++){
cin >> a[i].val >> a[i].weight >> a[i].quantity;
D[a[i].weight].push_back(i);
}
for (int i = 1; i <= s; i++){
if (!D[i].empty()) sort(D[i].begin(), D[i].end(), cmp);
}
for (int i = 1; i <= s; i++)
{
for (int j = 1; j <= s; j++)
{
if (!D[i].empty())
{
dp[i][j] = dp[i - 1][j];
int k = 1, dem = 1, ii = 0;
long long sum = 0;
while (j - k * i >= 0 && ii < D[i].size())
{
sum += a[D[i][ii]].val;
dp[i][j] = max(dp[i][j], dp[i - 1][j - k * i] + sum);
dem++; k++;
if (dem > a[D[i][ii]].quantity) dem = 1, ii++;
//cout << i << " " << j << " " << ii << " " << dp[i][j] << endl;
}
}
else dp[i][j] = dp[i - 1][j];
//cout << i << " " << j << " " << dp[i][j] << endl;
}
}
cout << dp[s][s];
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:35:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while (j - k * i >= 0 && ii < D[i].size())
| ~~~^~~~~~~~~~~~~
# | 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... |