이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
#define Created ios_base::sync_with_stdio(0);
#define By cin.tie(0);
#define Sofar cout.tie(0);
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
const long long int MOD=1e9+7, OO=0x3f3f3f3f;
const long long int LOO=0x3f3f3f3f3f3f3f3f;
const long double EPS=1e-8;
int main(){
Created By Sofar
int limit , types_amount;
cin >> limit >> types_amount;
map< int , vector<pi> > weights;
for(int i = 0 ; i < types_amount ; i++)
{
int value , its_weight , amount;
cin >> value >> its_weight >> amount ;
if(amount > 0 && its_weight <= limit)
{
weights[its_weight].pb({value,amount});
}
}
vector<vector<ll>>dp(
weights.size()+1 , vector<ll>(limit+1,INT32_MIN)
);
int k = 1;
dp[0][0] = 1;
for(auto &[w,items] : weights)
{
sort(items.begin(), items.end(), greater<pair<int, int>>());
for(int i = 0 ; i <= limit ; i++)
{
dp[k][i] = dp[k-1][i];
int copies = 0;
int type_at = 0;
int curr_used = 0;
long long profit = 0;
while ((copies + 1) * w <= i && type_at < items.size()) {
copies++;
profit += items[type_at].first;
if (dp[k - 1][i - copies * w] != INT32_MIN) {
dp[k][i] = std::max(
dp[k][i],
dp[k - 1][i - copies * w] + profit
);
}
curr_used++;
if (curr_used == items[type_at].second) {
curr_used = 0;
type_at++;
}
}
}
k++;
}
cout << *std::max_element(dp.back().begin(), dp.back().end())-1 << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:50:53: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | while ((copies + 1) * w <= i && type_at < items.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... |