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
2
2 1
10 10
1 1
2 2
8 10
2 3
Sample Output
10
21
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 ",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为每天增加的价值)
(贪心策略是斜率,算不上真正的斜率优化DP)
#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 ",dp[n][m]); } return 0; }