This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* - xét: f[i] = F[j] = 1
* => build 2 knap:
* + dp1[x]: mincost để mua được x cores
* + dp2[y]: max profit khi bán được y cores
* + ans = max(dp2[y] - dp1[x]) (x >= y)
* + cùng đơn vị core => ta sửa đổi lại lại một chút nhằm hợp lại còn 1 knap:
* + machine : c[i] cores và -v[i] value
* + order : -C[i] cores và V[i] value
* => knap max profit = max dp[i] (i >= 0)
*
* - xét: f, F bất kì
* => ta sort f, F (sort chung) giảm dần và build 1 knap dp[j] là max profit khi còn trữ j cores (j >= 0) (xét i ptu đầu)
*
* - x = n + m
* - đpt: O(xlogx + x * (n * c))
**/
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 2000 + 3, NC = N * 50;
int n, m;
struct Data {
int c, f, v;
bool operator < (Data o) const {
if (f != o.f) return f > o.f;
if (c != o.c) return c > o.c;
return v > o.v;
}
} a[N << 1];
ll dp[NC];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n;
for (int i = 0, c, f, v; i < n; ++i) {
cin >> c >> f >> v;
a[i] = { c, f, -v };
}
cin >> m;
for (int i = 0, c, f, v; i < m; ++i) {
cin >> c >> f >> v;
a[n + i] = { -c, f, v };
}
n += m;
sort(a, a + n);
memset(dp, 0x80, sizeof(dp)); dp[0] = 0;
int sum(0);
for (int i = 0, v, c; i < n; ++i) {
v = a[i].v, c = a[i].c;
if (c > 0) { // machine
sum += c;
for (int j = sum; j >= c; --j)
dp[j] = max(dp[j], dp[j - c] + v);
}
else { // order
c = -c;
for (int j = 0; j + c <= sum; ++j)
dp[j] = max(dp[j], dp[j + c] + v);
}
}
cout << *max_element(dp, dp + sum + 1) << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |