이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
struct itemtype {
int weight, cost, number;
};
bool cmp(const itemtype& x, const itemtype& y) {
if (x.weight != y.weight)
return x.weight < y.weight;
if (x.cost != y.cost)
return x.cost > y.cost;
if (x.number != y.number)
return x.number < y.number;
return true;
}
int main() {
int S, N; // maxweight, itemcount
cin>>S>>N;
itemtype types[N]; // weight, cost, number
for (int i = 0; i < N; i++) {
cin>>types[i].cost>>types[i].weight>>types[i].number;
}
sort(types, types + N, cmp);
vector<ii> items; // {weight, cost}
items.pb({-1, -1});
int currweight = -1;
int currneeded = -1;
for (int i = 0; i < N; i++) {
if (currneeded <= 0 && types[i].weight == currweight)
continue;
if (types[i].weight != currweight) {
currweight = types[i].weight;
currneeded = S;
}
while (types[i].number > 0 && currneeded >= currweight) {
items.pb({types[i].weight, types[i].cost});
types[i].number--;
currneeded -= currweight;
}
}
/*
for (int i = 0; i < items.size(); i++) {
cout<<"item "<<i<<": "<<items[i].first<<", "<<items[i].second<<endl;
}
*/
int dpbefore[S + 5];
for (int i = 0; i < S + 5; i++) {
dpbefore[i] = 0;
}
int dpafter[S + 5];
for (int i = 0; i < S + 5; i++) {
dpafter[i] = 0;
}
int maximum = 0;
for (int i = 1; i < (int)(items.size()); i++) {
copy(dpafter, dpafter + (S + 5), dpbefore);
for (int j = 1; j <= S; j++) {
dpafter[j] = dpbefore[j];
if (j >= items[i].first)
dpafter[j] = max(dpafter[j], dpbefore[j - items[i].first] + items[i].second);
// cout<<i<<" "<<j<<": "<<dp[i][j]<<endl;
maximum = max(maximum, dpafter[j]);
}
}
cout<<maximum<<endl;
}
# | 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... |