「CEOI2018」云计算

#3182. 「CEOI2018」云计算

Summarize

题目概括征集中~ 球球你再帮我写几篇吧 TAT @oykz2333

Solution

这题也太巧妙了叭! ——tth37

考虑类似背包的动态规划。记 $f[i]$ 表示满足订单后恰好剩余 $i$ 个核心的最大利润。

先不考虑时钟频率的限制条件,则问题可以转化为普通背包问题:将计算机的价格视为负数,将订单的核心数视为负数,采用背包问题的状态转移即可。

如果考虑到时钟频率的限制,则需要满足:在将每个订单放入背包之前,必须将所有时钟频率大于等于该订单的计算机放入背包中。

将计算机和订单按照时钟频率排序后,依次加入背包即可。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN = 2005;

struct Node {
int c, f, v;
bool type;
}a[MAXN * 2];

bool cmp(Node a, Node b) {
if (a.f != b.f) return a.f > b.f;
return a.type < b.type;
}

int n, m, tot;
ll f[MAXN * 50];

int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d%d%d", &a[i].c, &a[i].f, &a[i].v), a[i].type = 0, tot += a[i].c;
scanf("%d", &m);
for (int i = 1; i <= m; ++i)
scanf("%d%d%d", &a[n + i].c, &a[n + i].f, &a[n + i].v), a[n + i].type = 1;
sort(a + 1, a + n + m + 1, cmp);
memset(f, 0xcf, sizeof(f));
f[0] = 0;
for (int i = 1; i <= n + m; ++i) {
if (a[i].type == 0) {
for (int j = tot; j >= a[i].c; --j)
f[j] = max(f[j], f[j - a[i].c] - a[i].v);
} else {
for (int j = 0; j <= tot - a[i].c; ++j)
f[j] = max(f[j], f[j + a[i].c] + a[i].v);
}
}
ll ans = 0;
for (int i = 0; i <= tot; ++i)
ans = max(ans, f[i]);
printf("%lld", ans);
return 0;
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×