您的位置:首页 > 其它

ZOJ 3211 Dream City(DP)

2015-01-18 19:01 495 查看
思路:按b值从小到大排序,然后就是简单DP了,因为值越大的肯定放后面转移,所以是可行的

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 255;

int t, n, m;

struct Tree {
int a, b;
} tree
;

bool cmp(Tree a, Tree b) {
return a.b < b.b;
}

int dp

;

int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &tree[i].a);
for (int i = 1; i <= n; i++)
scanf("%d", &tree[i].b);
sort(tree + 1, tree + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + tree[i].a + tree[i].b * (j - 1));
}
}
printf("%d\n", dp
[m]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: