Dream City


Time Limit: 1 Second      Memory Limit:32768 KB

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[];
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=;i<=n;i++)
scanf("%d",&L[i].a);
for(int i=;i<=n;i++){
scanf("%d",&L[i].b);
L[i].used=false;
}
}
void _solve()
{
for(int i=m;i>=;i--)
{
for(int j=;j<=n;j++){
if(!L[j].used) L[j].c=L[j].a+L[j].b*(i-);
else L[j].c=;
}
sort(L+,L+n+,cmp);
ans+=L[].c;
L[].used=true;
}
printf("%lld\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
ans=;
_in();
_solve();
}
return ;
}

 

因为考虑到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[][]; //前i课树,第j天所能取到的最大值
struct in
{
int a,b;
}tree[];
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 = ; i <= n; i++) tree[i].a=_S();
for (int i = ; i <= n; i++) tree[i].b=_S();
sort(tree + ,tree + n + ,cmp); //dp是算出取哪几颗树,但是这几棵树的砍得顺序不一定。砍得顺序不同,导致结果不同。所以先处理好顺序。
for (int i = ; i <= n; i++)
{
for (int j = ; j <= min(i,m); j++) //看网上很多代码都是算到m其实到min(i,m)就可以。
{
dp[i][j] = max(dp[i - ][j],dp[i - ][j - ] + tree[i].a + tree[i].b * (j - )); //第i棵树取或者不取
}
}
printf("%d\n",dp[n][m]);
}
return ;
}

zoj3211dream city dp 斜率的更多相关文章

  1. 【BZOJ-4518】征途 DP + 斜率优化

    4518: [Sdoi2016]征途 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 230  Solved: 156[Submit][Status][ ...

  2. 【BZOJ-3437】小P的牧场 DP + 斜率优化

    3437: 小P的牧场 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 705  Solved: 404[Submit][Status][Discuss ...

  3. 【BZOJ-1010】玩具装箱toy DP + 斜率优化

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8432  Solved: 3338[Submit][St ...

  4. 【BZOJ】1096: [ZJOI2007]仓库建设(dp+斜率优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1096 首先得到dp方程(我竟然自己都每推出了QAQ)$$d[i]=min\{d[j]+cost(j+ ...

  5. BZOJ 1096: [ZJOI2007]仓库建设(DP+斜率优化)

    [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在 ...

  6. 学渣乱搞系列之dp斜率优化

    学渣乱搞系列之dp斜率优化 By 狂徒归来 貌似dp的斜率优化一直很难搞啊,尤其是像我这种数学很挫的学渣,压根不懂什么凸包,什么上凸下凸的,哎...说多了都是泪,跟wdd讨论了下,得出一些结论.本文很 ...

  7. DP斜率优化总结

    目录 DP斜率优化总结 任务安排1 任务计划2 任务安排3 百日旅行 DP斜率优化总结 任务安排1 首先引入一道题,先\(O(N^2)\)做法:分别预处理出\(T_i,C_i\)前缀和\(t[i],c ...

  8. HDU 3507 [Print Article]DP斜率优化

    题目大意 给定一个长度为\(n(n \leqslant 500000)\)的数列,将其分割为连续的若干份,使得 $ \sum ((\sum_{i=j}^kC_i) +M) $ 最小.其中\(C_i\) ...

  9. dp斜率优化

    算法-dp斜率优化 前置知识: 凸包 斜率优化很玄学,凭空讲怎么也讲不好,所以放例题. [APIO2014]序列分割 [APIO2014]序列分割 给你一个长度为 \(n\) 的序列 \(a_1,a_ ...

随机推荐

  1. 结对编程-四则运算-GUI

     201421123022 王若凡        201421123026  欧阳勇 https://git.coding.net/ttoyy/sizeyunsuan-GUI.git a.需求分析: ...

  2. 结对编程1--基于GUI的四则运算题目生成器

    最终项目详细代码(coding地址)/李志强的博客/我的详细代码/方言的博客 一.需求分析 1.实现之前项目的功能,如:除了整数之外,还要支持真分数的四则运算,真分数的运算,并且要求能处理用户的输入, ...

  3. spring+mybatis的简单配置示例

    简单代码结构: //Book.java package com.hts.entity; public class Book { private String id; private String bo ...

  4. 201521123090 《Java程序设计》第7周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代 ...

  5. 201521123089 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 总结: (1)类名的首字母一定要大写. (2)制类型转换:类强制转换为子类时只有当引用类型真 ...

  6. 201521123024 《Java程序设计》第1周学习总结

    一.本周章学习总结 1.Java的版本迁移 2.运用eclipse和notepad++编写Java 3.对JDK,JRE,JVM有初步的了解 二.书面作业 1.为什么java程序可以跨平台运行?执行j ...

  7. 本地不安装oracle,plsql照样用起来

    对于配置有限的电脑,不安装oracle需要使用plsql怎么设置才可以使用呢 一.首先下载一个instantclient (Oracle提供的一个较为轻量级的客户包) 据说plsql不支持64位的in ...

  8. Servlet第三篇【request和response简介、response的常见应用】

    response.request对象 Tomcat收到客户端的http请求,会针对每一次请求,分别创建一个代表请求的request对象.和代表响应的response对象 既然request对象代表ht ...

  9. Hibernate table schema 的设置与应用

    hibernate在实现实体映射时,DB无需强行指定.部署时会较对DB户名和密码,根据用户名以访问的表完成实体映射.如果一个帐号可以访问一个数据库的下多个表,以oracle为例用户user1下面有表t ...

  10. OSGi-开发环境的建立和HelloWorld(04)

    1 OSGi开发环境的建立 1.1 Equinox是什么 从代码角度来看,Equinox其实就是OSGi核心标准的完整实现,并且还在这个基础上增加了一些额外的功能(比如为框架增加了命令行和程序执行的入 ...