日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【ZOJ - 3211】Dream City (01背包类问题,贪心背包)

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ZOJ - 3211】Dream City (01背包类问题,贪心背包) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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?mor 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.

?

?

Input

?

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)

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

Hint

s:
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]代表第i填砍了前j棵樹,你i天肯定砍了i棵樹,那么剩下的肯定都從后面砍的,所以涉及到一個排序問題,而且這個狀態定義會有后效性,所以pass掉。

正解:dp[i][j]代表考慮對于第i棵樹,總共砍了j棵樹(或者砍了j天),的最大價值,那么此時有選和不選兩種狀態,分別轉移。問題轉化成:也就是我有一個體積為m的背包,n個物品,每個物品體積為1,并且物品的價值隨放入順序而改變,問你最優放入順序(最大價值)。

那么這種問題肯定要貪心排序,處理掉“物品的價值會隨時間改變而改變” 這一要素,對于這題假設我們選了m種物品,那么肯定b越大的越后選。所以我們按照b排序。這樣就是n個物品選m個物品的最優解問題了,01背包解決。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 500 + 5;int n,m; set<int> ss[MAX][MAX]; ll val[MAX][MAX];//第i棵樹 第j天 的val struct Node {int a,b;bool operator<(const Node c)const{return b < c.b;} } node[MAX]; ll dp[MAX]; int main() {int t;cin>>t;while(t--) {scanf("%d%d",&n,&m);memset(dp,-0x3f,sizeof dp);for(int i = 1; i<=n; i++) scanf("%d",&node[i].a);for(int i = 1; i<=n; i++) scanf("%d",&node[i].b);memset(val,0,sizeof val);sort(node+1,node+n+1);for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(j == 1) val[i][j] = node[i].a;else val[i][j] = val[i][j-1] + node[i].b;}}dp[0]=0;for(int i = 1; i<=n; i++) {for(int j = m; j>=1; j--) {dp[j] = max(dp[j],dp[j-1] + val[i][j]);}}printf("%lld\n",dp[m]);}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【ZOJ - 3211】Dream City (01背包类问题,贪心背包)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。