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;
}
代码:
#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;
}
相关文章推荐
- ZOJ-3211-Dream City-DP背包问题+贪心
- ZOJ 3211 Dream City【dp】
- ZOJ 3211 Dream City(DP)
- zoj 3211 Dream City(DP)
- ZOJ 3211 Dream City(dp)
- ZOJ-3211-Dream City【6th浙江省赛】【dp】
- zoj 3211 dream city 贪心 dp和记忆化搜索
- ZOJ 3211 6th省赛 J Dream City【DP】
- ZOJ 3211 Dream City(DP)
- ZOJ 3211 Dream City DP 01背包 经典问题
- ZOJ 3211 Dream City (J) DP
- ZOJ 3211 Dream City(线性DP)
- ZOJ 3211 Dream City
- zoj 3211 Dream City
- ZOJ 3211 Dream City
- ZOJ - 3211 Dream City
- ZOJ3206 Disaster Area Reconstruction ZOJ 3211 Dream City ZOJ 3212 K-Nice
- zoj 3211 砍树 有顺序的dp
- zoj 3211 Dream City 动态规划
- zoj 3211 Dream City 贪心+动规