Dream City ZOJ
来源:互联网 发布:河北大学工商学院 知乎 编辑:程序博客网 时间:2024/06/05 20:43
JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are ntrees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins on it (i=1, 2, 3...n). Surprisingly, each tree i can growbi new coins each day if it is not cut down. From the first day, JAVAMAN can choose to cut down one tree each day to get all the coins on it. Since he can stay in the Dream City for at most m days, he can cut down at most m trees in all and if he decides not to cut one day, he cannot cut any trees later. (In other words, he can only cut down trees for consecutive m or less days from the first day!)
Given n, m, ai and bi (i=1, 2, 3...n), calculate the maximum number of gold coins JAVAMAN can get.
There are multiple test cases. The first line of input contains an integer T (T <= 200) indicates the number of test cases. Then T test cases follow.
Each test case contains 3 lines: The first line of each test case contains 2 positive integers n and m (0 < m <= n <= 250) separated by a space. The second line of each test case contains n positive integers separated by a space, indicating ai. (0 < ai <= 100, i=1, 2, 3...n) The third line of each test case also contains npositive integers separated by a space, indicating bi. (0 < bi <= 100, i=1, 2, 3...n)
<b< dd="">
For each test case, output the result in a single line.
<b< dd="">
22 110 101 12 28 102 3
<b< dd="">
1021
<b< dd="">
Test case 1: JAVAMAN just cut tree 1 to get 10 gold coins at the first day.
Test case 2: JAVAMAN cut tree 1 at the first day and tree 2 at the second day to get 8 + 10 + 3 = 21 gold coins in all.
一道比較明显的动态规划,需要注意的是树砍掉之后就不长了,还有需要提前根据树的增长率进行排序,因为要先砍增长率低的树,至于为什么,可以自己想一下。
然后就是状态转移方程,dp[i][j]表示前i棵树在前j天得到的最大值,
dp[i][j] = max( dp[i-1][j] , dp[i-1][j-1] + chushi[i] + zengzhangzhi[i]*(j-1) );
上代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<string>#include<set>using namespace std;struct node{int x;int y;int tmp;};node op[252];int dp[252][252];bool cmp(node a,node b){return a.y < b.y;}int main(){ int t; scanf("%d", &t); while(t--) { int n,m; scanf("%d %d",&n,&m); for(int i=1; i<=n; i++) { scanf("%d", &op[i].x); //op[i].tmp = op[i].x; } for(int i=1; i<=n; i++) { scanf("%d", &op[i].y); } memset(dp, 0, sizeof(dp)); sort(op+1, op+1+n,cmp); for(int i=1; i<=n; i++) { for(int j=1; j<=i; j++) { dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]+op[i].x+op[i].y*(j-1)); } } printf("%d\n", dp[n][m]); } return 0;}水波.
- ZOJ - 3211 Dream City
- zoj 3211 - Dream City
- ZOJ 3211 Dream City
- ZOJ 3211 Dream City
- zoj 3211 Dream City
- Dream City ZOJ
- ZOJ 3211 Dream City(DP)
- ZOJ 3211Dream City(dp)
- ZOJ 3211 Dream City【dp】
- zoj 3211 Dream City 动态规划
- zoj 3211 Dream City(DP)
- zoj 3211 Dream City 贪心+动规
- ZOJ 3211 Dream City(DP)
- zoj 3211 Dream city dp+贪心
- Dream City
- ZOJ-3211-Dream City【6th浙江省赛】【dp】
- ZOJ 3211 6th省赛 J Dream City【DP】
- zoj 3211 dream city 贪心 dp和记忆化搜索
- ACM训练日记—8月5日
- Hibernate学习5 事务管理简单例子购买股票
- Cocos2d-x实战项目开发:运动的小球
- javascript 实现父节点下子节点的倒序排列
- PYTHON机器学习实战——朴素贝叶斯
- Dream City ZOJ
- sstream与相比stdio.h的好处
- java mooc第三周对象容器
- Are We There Yet? (zoj1745)
- 用C++写一个日历程序,要求输入年份,显示全年的日历
- poj 1006 中国剩余定理模板
- tinker热修复问题
- 存储过程调试
- Java中文件的随机读写