贪心,$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. 【CodeForces】788E New task

    [题意]n个数,每个数有附加属性0或1,初始全为1.m个操作,每个操作可以改变一个数字的属性为0或1.对于每次操作后的序列求有多少子序列满足要求:5个数字,中间3个数相等且属性为1,左右两个数小于等于 ...

  2. python keras YOLOv3实现目标检测

    1.连接 https://www.jianshu.com/p/3943be47fe84

  3. React组件生命周期小结

    React组件生命周期小结 下面所写的,只适合前端的React.(React也支持后端渲染,而且和前端有点小区别,不过我没用过.) 相关函数 简单地说,React Component通过其定义的几个函 ...

  4. 【HNOI】五彩斑斓 模拟

    [题目描述] 给定一个n*m的矩阵,矩阵的某些位置有一个颜色(可以没有颜色,即为0),现在你可以将矩阵的某一行或者某一列染成同一种颜色,问最少用多少步能达到目标矩阵的染色方案,输出最少步数和方案. [ ...

  5. [干货,阅后进BAT不是梦]面试心得与总结---BAT、网易、蘑菇街

    本文转载自:公众号:JANiubility 前言 之前实习的时候就想着写一篇面经,后来忙就给忘了,现在找完工作了,也是该静下心总结一下走过的路程了,我全盘托出,奉上这篇诚意之作,希望能给未来找工作的人 ...

  6. python自动开发之第二十四天(Django)

    一.ModelForm操作及验证 1.class Meta:class Meta: #注意以下字段不能加逗号 model = models.UserInfo #这里的all代指所用的字段,也可以是一个 ...

  7. Django rest framework + Vue简单示例

    构建vue项目参考这篇文章https://segmentfault.com/a/1190000008049815 一.创建Vue项目 修改源:npm config set registry https ...

  8. 比 file_get_contents() 更优的 cURL 详解(附实例)

    PHP 可以使用 file_get_content() 函数抓取网页内容,但却无法进行更复杂的处理,譬如文件的上传或下载. Cookie 操作等等.而 cURL 提供了这些功能. 一.cURL简介 在 ...

  9. Laravel 调试器 Debugbar 和数据库导出利器 DbExporter 扩展安装及注意事项

    一.Debugbar安装 参考:Laravel 调试利器 —— Laravel Debugbar 扩展包安装及使用教程 的“2.安装”部分 二.DbExporter安装 参考:Laravel 扩展推荐 ...

  10. centos 挂在ntfs

    Installing build-essentials in CentOS (make, gcc, gdb):http://www.techblogistech.com/2012/03/install ...