2821: Dream City

描述

JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees 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 grow bi 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 nmai 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 n positive integers separated by a space, indicating bi. (0 < bi <= 100, i=1, 2, 3...n)

输出

For each test case, output the result in a single line.

样例输入

2
2 1
10 10
1 1
2 2
8 10
2 3

样例输出

10
21

提示

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.
题意:有n颗树,编号1~n,第i棵树初始有ai个硬币,而且每天会增长bi个硬 币,现在可以每天砍倒一棵树并获得树上所有硬币,问m天最多可以获得多少硬币
思路:DP,dp[ i ][ j ]表示 j 天砍前 i 棵最多可以获得多少硬币。
每棵树按照bI从小到大排序,bi相同按ai从小到大排序。
状态转移方程:dp[i][j]=max(dp[ i-1 ][ j ],dp[ i-1 ][ j-1 ]+.ai+.bi * ( j-1 ));
#include<bits/stdc++.h>
using namespace std;
struct node{
   int a,b;

}e[255];
int dp[255][255];
int cmp(node x,node y){
    return x.b<y.b||x.b==y.b&&x.a<y.a;
}
int main(){
      int t,n,m;
      cin>>t;
      while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++)cin>>e[i].a;
        for(int i=1;i<=n;i++)cin>>e[i].b;
        sort(e+1,e+n+1,cmp);
        for(int j=1;j<=m;j++){
            for(int i=1;i<=n;i++){
                dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+e[i].a+e[i].b*(j-1));
            }
        }
        cout<<dp[n][m]<<endl;
      }
}
View Code

 

 

posted on 2020-05-28 13:03  还是力量不够  阅读(167)  评论(0)    收藏  举报

导航