zoj3211dream city dp 斜率
来源:互联网 发布:苏联级战列舰知乎 编辑:程序博客网 时间:2024/05/22 11:41
JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There aren trees in the yard. Let's call them tree 1, tree 2 ...and treen. At the first day, each treei hasai coins on it (i=1, 2, 3...n). Surprisingly, each treei 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 mostm days, he can cut down at mostm 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 consecutivem or less days from the first day!)
Given n,m,ai andbi (i=1, 2, 3...n), calculate the maximum number of gold coins JAVAMAN can get.
Input
There are multiple test cases. The first line of input contains an integerT (T <= 200) indicates the number of test cases. ThenT test cases follow.
Each test case contains 3 lines: The first line of each test case contains 2 positive integersn andm (0 <m <=n <= 250) separated by a space. The second line of each test case containsn positive integers separated by a space, indicatingai. (0 <ai <= 100,i=1, 2, 3...n) The third line of each test case also containsn positive integers separated by a space, indicatingbi. (0 <bi <= 100,i=1, 2, 3...n)
Output
For each test case, output the result in a single line.
Sample Input
22 110 101 12 28 102 3
Sample Output
1021
Hints:
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.
1,排序问题?按a和b的什么关系排?好像不行
2,贪心?试试。于是得到下面这个错误代码
#include<cstdio>#include<cstdlib>#include<iostream>#include<map>#include<memory.h>#include<iostream>#include<algorithm>using namespace std;struct in{int a,b,c;bool used;}L[251];bool cmp(in a,in b){if(a.c==b.c) return a.b>b.b;return a.c>b.c;}long long ans;int n,m;void _in(){scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) scanf("%d",&L[i].a);for(int i=1;i<=n;i++){scanf("%d",&L[i].b); L[i].used=false;}}void _solve(){for(int i=m;i>=1;i--){for(int j=1;j<=n;j++){ if(!L[j].used) L[j].c=L[j].a+L[j].b*(i-1); else L[j].c=0;}sort(L+1,L+n+1,cmp);ans+=L[1].c;L[1].used=true;}printf("%lld\n",ans);}int main(){int T;scanf("%d",&T);while(T--){ ans=0;_in();_solve();}return 0;}
因为考虑到a小的b可能大,会影响到后面的选择,于是此贪心是的思想是从后向前。但错误样例:
a:1 100
b:99 1
如果倒着选(100+1)+1则小于 (99+1)+100;
但是之前做过斜率问题的司机们应该知道这道题的后续性不是倒着选来控制,而是按斜率排序来操作(词穷了)。
具体的:斜率小的物品,如果在某个阶段不选,那么在以后都不会再选。因为它增长得慢,如果在开始的时候没选它,那到后面别人长得比它快就肯定轮不到它了。
然后,可将问题转化为01背包问题。dp[i][j]表示前i课树,第j天所能取到的最大值。
状态转移方程 : dp[i][j] = max(dp[i-1][j], dp[i-1][j-1] + tree[i].a + (j-1)*tree[i].b)// (tree[i].a为初始值,tree[i].b为每天增加的价值)
2017-08-09 16:06:12C++0532
2017-08-09 16:06:12C++0ms532kb by fcy6#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;int dp[251][251]; //前i课树,第j天所能取到的最大值 struct in{ int a,b;}tree[251];bool cmp(in x,in y){ return x.b < y.b; };int main(){ int t; t=_S(); while(t--) { int n,m; n=_S(); m=_S(); for (int i = 1; i <= n; i++) tree[i].a=_S(); for (int i = 1; i <= n; i++) tree[i].b=_S(); sort(tree + 1,tree + n + 1,cmp); //dp是算出取哪几颗树,但是这几棵树的砍得顺序不一定。砍得顺序不同,导致结果不同。所以先处理好顺序。 for (int i = 1; i <= n; i++) { for (int j = 1; j <= min(i,m); j++) //看网上很多代码都是算到m其实到min(i,m)就可以。 { dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - 1] + tree[i].a + tree[i].b * (j - 1)); //第i棵树取或者不取 } } printf("%d\n",dp[n][m]); } return 0;}
from代码出处。将_S换成scanf或者cin即可!
- zoj3211dream city dp 斜率
- 斜率dp
- 【DP】 hdu1505 City Game
- 【bzoj3125】CITY 插头dp
- DP(斜率优化)
- 【斜率优化DP】Batch_Scheduling
- dp优化--斜率
- 斜率优化DP
- 斜率优化DP
- hdu3507斜率优化dp
- DP斜率优化总结
- hdu3480 斜率优化dp
- hdu3045之斜率DP
- hdu3507 斜率优化dp
- 斜率优化DP
- 斜率dp模板
- 斜率优化DP 【pascal】
- 斜率优化DP
- python常用模块
- 两个栈实现队列
- 51 nod 1133 二级算法题 贪心
- android 安装内核module,提示Required key not available
- OpenSessionInView 的作用、配置及原理
- zoj3211dream city dp 斜率
- 数据结构笔记——队列
- fetch获取本地json文件问题
- 总账报表
- 如何解决Xshell中文乱码问题
- Koa框架教程,Koa框架开发指南,Koa框架中文使用手册,Koa框架中文文档
- 三.python程序控制结构
- [leetcode] 12. Integer to Roman
- phpstudy APACHE支持.htaccess以及 No input file specified解决方案