ZOJ 3211 Dream City
贪心,$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的更多相关文章
- ZOJ 3211 Dream City(线性DP)
Dream City Time Limit: 1 Second Memory Limit: 32768 KB JAVAMAN is visiting Dream City and he se ...
- ZOJ 3211 Dream City(DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3374 题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上 ...
- ZOJ 3211 Dream City DP 01背包 经典问题
题目大意:JAVAMAN 到梦幻城市旅游见到了黄金树,黄金树上每天回结出金子.已经有n棵树,JAVAMAN要停留m天,每天只能砍掉一棵树,砍掉树后就能得到树上的黄金.给定n棵树上原有的黄金a[i]和每 ...
- Dream City(线性DP)
描述 JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the ya ...
- ZOJ Design the city LCA转RMQ
Design the city Time Limit: 1 Second Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...
- zoj 4122 Triangle City 2019山东省赛J题
题目链接 题意: 给出一个无向图,类似三角形的样子,然后给出边的权值,问找一条从第一个点到最后一个点的路径,要求每一条边只能走一次,并且权值和最大,点可以重复走. 思路: 首先观察这个图可以发现,所有 ...
- ZOJ 3211dream city dp(效率优化)
Dream City Time Limit: 1 Second Memory Limit:32768 KB JAVAMAN is visiting Dream City and he see ...
- ZOJ3211-Dream City(贪心思想+变形的01背包)
Dream City Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Sta ...
- zoj3211dream city dp 斜率
Dream City Time Limit: 1 Second Memory Limit:32768 KB JAVAMAN is visiting Dream City and he see ...
随机推荐
- 页面自适应<meta name="viewport">标签设置
viewport: 它在页面中设置,是应对手机模式访问网站.网页对屏幕而做的一些设置.通常手机浏览器打开页面后,会把页面放在一个虚拟的“窗 口”–这个比窗口大,也就是你常发现页面可以进行拖动.放大放小 ...
- rocketmq单机搭建
RocketMQ 是alibaba开源的消息队列. 本文使用的是开源版本v3.18 系统: centos6.x最小化安装 需要用到的软件包: jdk-7u67-linux-x64.tar.gz ali ...
- [Luogu 1533] 可怜的狗狗
平衡树,我用的SBT. 排一下序尽量减少操作次数. 第K大询问. 以及插入删除. #include <algorithm> #include <cstdio> #include ...
- 使用Apache Curator监控Zookeeper的Node和Path的状态
1.Zookeeper经常被我们用来做配置管理,配置的管理在分布式应用环境中很常见,例如同一个应用系统需要多台 PC Server 运行,但是它们运行的应用系统的某些配置项是相同的,如果要修改这些相同 ...
- 51nod 1106 质数检测——Mr判素数
质数检测一般都是根号n的写法 当然Mr判素数的方法可以实现log的复杂度2333 Mr判素数的话 我们根据费马小定理只要P是素数 那么另一个素数x 满足 x^P-1≡1(mod P) 同时 x^2%P ...
- 【洛谷 P2742】【模板】二维凸包
题目链接 二维凸包板子..有时间会补总结的. #include <cstdio> #include <cmath> #include <algorithm> usi ...
- 首行缩进css
html首行缩进2字符,可以使用CSS属性中的[text-indent]进行设置. 设置代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...
- Python 16进制与字符串的转换
电脑上装了Python2.7和3.3两个版本,平时运行程序包括在Eclipse里面调试都会使用2.7,但是由于某些原因在cmd命令行中输入python得到的解释器则是3.3, 一直没对此做处理,因为这 ...
- ew做socks5代理
这个工具和之前讲过的xxoo类似.链接:https://www.cnblogs.com/nul1/p/8883271.html https://zhuanlan.zhihu.com/p/3282215 ...
- Bookmarks www
Bookmarks alexis- (Alex Incogito) - Repositories · GitHub GitHub - aetcnc-Arduino_DeltaHMI_RS485 Ope ...