贪心,$dp$。

假设我们知道要选择哪些物品,那么这些物品应该按什么顺序选择呢?

物品$A(a1,b1)$,物品$B(a2,b3)$。

假设物品$A$在第$x$天被选择,物品$B$在第$y$天被选择。$x<y$。那么收益为:$P1=a1+(x-1)*b1+a2+(y-1)*b2$。

假设物品$A$在第$y$天被选择,物品$B$在第$x$天被选择。$x<y$。那么收益为:$P2=a1+(y-1)*b1+a2+(x-1)*b2$。

$P1>P2$的条件为:$(x-1)*b1+(y-1)*b2>(y-1)*b1+(x-1)*b2$,即$(y-x)*(b2-b1)>0$,即$b2>b1$。

也就是说,假设我们知道要选择哪些物品,那么这些物品按$b$从小到大取获得的收益最大。

因此,我们可以降物品按照$b$从小到大排序,然后去决策应该选择哪些物品,$dp$即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int T,n,m; struct X
{
int a,b;
}s[]; bool cmp(X a, X b)
{
return a.b<b.b;
} int dp[][]; int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&s[i].a);
for(int i=;i<=n;i++) scanf("%d",&s[i].b); sort(s+,s++n,cmp); memset(dp,,sizeof dp); dp[][]=s[].a;
for(int i=;i<=n;i++) dp[i][]=max(dp[i-][],s[i].a); for(int j=;j<=m;j++)
{
for(int i=j;i<=n;i++)
{
dp[i][j]=max(dp[i-][j],dp[i-][j-]+s[i].a+(j-)*s[i].b);
}
} printf("%d\n",dp[n][m]); }
return ;
}

ZOJ 3211 Dream City的更多相关文章

  1. ZOJ 3211 Dream City(线性DP)

    Dream City Time Limit: 1 Second      Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he se ...

  2. ZOJ 3211 Dream City(DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3374 题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上 ...

  3. ZOJ 3211 Dream City DP 01背包 经典问题

    题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上每天回结出金子.已经有n棵树,JAVAMAN要停留m天,每天只能砍掉一棵树,砍掉树后就能得到树上的黄金.给定n棵树上原有的黄金a[i]和每 ...

  4. Dream City(线性DP)

    描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...

  5. ZOJ Design the city LCA转RMQ

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  6. zoj 4122 Triangle City 2019山东省赛J题

    题目链接 题意: 给出一个无向图,类似三角形的样子,然后给出边的权值,问找一条从第一个点到最后一个点的路径,要求每一条边只能走一次,并且权值和最大,点可以重复走. 思路: 首先观察这个图可以发现,所有 ...

  7. ZOJ 3211dream city dp(效率优化)

    Dream City Time Limit: 1 Second      Memory Limit:32768 KB JAVAMAN is visiting Dream City and he see ...

  8. ZOJ3211-Dream City(贪心思想+变形的01背包)

    Dream City Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Sta ...

  9. zoj3211dream city dp 斜率

    Dream City Time Limit: 1 Second      Memory Limit:32768 KB JAVAMAN is visiting Dream City and he see ...

随机推荐

  1. java线程的常用方法和属性介绍

    start() start方法是Thread 类的方法,在这个方法中会调用native方法(start0())来启动线程,为该线程分配资源. sleep() sleep方法有2个方法. public ...

  2. 51Nod 1062 序列中最大的数 | 简单DP

    #include "iostream" #include "cstdio" using namespace std; #define LL long long ...

  3. Mybatis xml 写sql如何判断集合的size

    在mybtis的映射文件中判断集合大小  list.size  例子如下: <if test="groupIds != null and groupIds.size>0" ...

  4. MyBatis框架的使用及源码分析(七) MapperProxy,MapperProxyFactory

    从上文<MyBatis框架中Mapper映射配置的使用及原理解析(六) MapperRegistry> 中我们知道DefaultSqlSession的getMapper方法,最后是通过Ma ...

  5. 使用awk批量杀进程的命令

    在做系统运维的过程中,有时候会碰到需要杀掉某一类进程的时候,如何批量杀掉这些进程,使用awk命令是很好的选择. ps -ef|grep aaa|grep -v grep|awk '{print &qu ...

  6. sass_sass安装

    你会不会因为有些事遇到各种各样的问题而搁置,直到把这个事情被耽误了几天.最近一直在弄sass这个东西,安装的过程中各种问题.sass是一个基于ruby环境开发的,安装sass之前得先把ruby给安装了 ...

  7. 记一次Powershell反混淆 (1)

    样本地址: https://www.virustotal.com/#/file/6f9034646e6fcead5342f708031412e3c2efdb4fb0f37bba43133a471d1c ...

  8. clientX,offsetX,layerX,pageX,screenX,X鼠标位置全解

    clientX,offsetX,layerX,pageX,screenX,X有时容易记混,通过测试当前的主流浏览器疏理了自己的一些看法以供参考. Chrome下(测试版本为51.0.2704.106  ...

  9. 更新T1表,要添加一个条件A,但T1表没有A字段

    可以这样 如果T2表有A字段,T1.T2表有共同字段B,可以通过T2表A字段为条件查得B字段 再用B字段为条件去更新T1表 update T1 set C = '1' where B in(selec ...

  10. 创建.dat文件(转载)

    比较有用的东比较有用的东西 首先,批处理文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行的命令行一样),你可以使用DOS下的Edit或者Windows的 ...