ZOJ3211-Dream City(贪心思想+变形的01背包)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
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 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
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.
有n颗树,每棵树都有固定的初始值,和有增长值,
给出m天,每天只能砍一棵树,那么对与第j天,
如果选择砍掉第i棵树,
那么收获的硬币就是初始值+增长值*(j-1),
怎样砍才能使收获的硬币最多
在做背包前,要先排个序,
可将问题转化为01背包问题。
dp[i][j]表示前i课树,第j天所能取到的最大值。
状态转移方程 :
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+p[i].v1+p[i].v2*(j-1));
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define eps 10e-6
#define INF 999999999
using namespace std;
#define max_v 255
struct node
{
int v1;
int v2;
}p[max_v];
bool cmp(node a,node b)
{
return a.v2<b.v2;
}
int dp[max_v][max_v];
int main()
{
int t;
int n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m); for(int i=;i<=n;i++)
{
scanf("%d",&p[i].v1);
}
for(int i=;i<=n;i++)
{
scanf("%d",&p[i].v2);
} sort(p+,p++n,cmp); memset(dp,,sizeof(dp)); int ans=-INF;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
dp[i][j]=max(dp[i-][j],dp[i-][j-]+p[i].v1+p[i].v2*(j-));
ans=max(ans,dp[i][j]);
}
}
printf("%d\n",ans);
}
return ;
}
/* 题目意思:
有n颗树,每棵树都有固定的初始值,和有增长值,
给出m天,每天只能砍一棵树,那么对与第j天,
如果选择砍掉第i棵树,
那么收获的硬币就是初始值+增长值*(j-1),
怎样砍才能使收获的硬币最多 解题思路:增长快的要放在后面砍会比较有利。
在做背包前,要先排个序,
可将问题转化为01背包问题。
dp[i][j]表示前i课树,第j天所能取到的最大值。
状态转移方程 :
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+p[i].v1+p[i].v2*(j-1)); */
ZOJ3211-Dream City(贪心思想+变形的01背包)的更多相关文章
- 3466 ACM Proud Merchants 变形的01背包
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:假设你有M元,已经Pi,Qi,Vi(i为角标,1<i<N),当M>Qi,时才 ...
- 背包问题(01背包,完全背包,多重背包(朴素算法&&二进制优化))
写在前面:我是一只蒟蒻~~~ 今天我们要讲讲动态规划中~~最最最最最~~~~简单~~的背包问题 1. 首先,我们先介绍一下 01背包 大家先看一下这道01背包的问题 题目 有m件物品和一个容量为 ...
- 01背包--hdu2639
hdu-2639 The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rooki ...
- 【牛客-14602】xinjun与阴阳师(01背包)
xinjun与阴阳师 题目描述 xinjun是各类手游的狂热粉丝,因随手一氪.一氪上千而威震工大,现在他迷上了阴阳师.xinjun玩手游有一个习惯,就是经过层层计算制定出一套方案来使操作利益最大化(因 ...
- HDU 3446 有贪心思想的01背包
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- HDU -2546饭卡(01背包+贪心)
这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择 ...
- 0-1背包的动态规划算法,部分背包的贪心算法和DP算法------算法导论
一.问题描述 0-1背包问题,部分背包问题.分别实现0-1背包的DP算法,部分背包的贪心算法和DP算法. 二.算法原理 (1)0-1背包的DP算法 0-1背包问题:有n件物品和一个容量为W的背包.第i ...
- Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...
- hdu–2369 Bone Collector II(01背包变形题)
题意:求解01背包价值的第K优解. 分析: 基本思想是将每个状态都表示成有序队列,将状态转移方程中的max/min转化成有序队列的合并. 首先看01背包求最优解的状态转移方程:\[dp\left[ j ...
随机推荐
- SpringMVC接受请求参数、
1. 接收请求参数 1.1. [不推荐]通过HttpServletRequest 在处理请求的方法中,添加HttpServletRequest对象作为参数,在方法体中,直接调用参数对象的getPara ...
- p2p项目工具类
1.用于存放当前用户的上下文UserContext package com.xmg.p2p.base.util; import javax.servlet.http.HttpSession; impo ...
- arcgis英文版补丁下载地址
http://support.esri.com/zh-cn/downloads/patches-servicepacks/list/productid/160
- 日期选择器(DatePicker)
日期选择器(DataPicker) 显示一个可供日期选择的界面 监听器方法init(year,month,day,OnDateChangedListener) 监听器 DataPicker.OnDat ...
- 为什么不要使用 select * from xxx (oracle 亲测)
打开已用时间set timing on;create table users(id number(20), name varchar2(20), password varchar2(20));inse ...
- 【Java】数组使用
package aaa; public class aaa { public static void main(String args[]) { int a[]={1,2,3,4}; for(int ...
- 使用 NGINX 进行微程序缓存的好处
[编者按]本文作者为 Owen Garrett,主要介绍使用 nginx 进行微程序缓存的好处,辅之以生动的实例.文章系国内 ITOM 管理平台 OneAPM 编译呈现. NGINX 和 NGINX ...
- Ext根据条件显示隐藏列
Ext根据条件显示隐藏列 写在ExtonReady函数里面,并在表格成功渲染之后,可以添加判断是否隐藏或者显示某一列 /* 判断是否显示版本号一列 */ var showVersionFlag = ' ...
- libgdx自问自答
1.使用gdx-steup.jar生成的desktop项目导入idea运行报如下错误,如何解决? 答:原因是assets目录默认是普通目录,idea编译项目时不会把普通目录下的内容输出到classpa ...
- LeetCode题解之Hamming Distance
1.题目描述 2.问题分析 使用C++ 标准库中的 bitset 类,将整数转换为二进制形式,然后再将其转换为字符串,最后比较字符串. 3.代码 int hammingDistance(int x, ...