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 ...
随机推荐
- 使用 log4j 在控制台 打印 hibernate 语句参数
log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.st ...
- UVA 1213 Sum of Different Primes
https://vjudge.net/problem/UVA-1213 dp[i][j][k] 前i个质数里选j个和为k的方案数 枚举第i个选不选转移 #include<cstdio> # ...
- 两台linux服务器之间免密scp,在A机器上向B远程拷贝文件
两台linux服务器之间免密scp,在A机器上向B远程拷贝文件 操作步骤:1.在A机器上,执行ssh-keygen -t rsa,一路按Enter,不需要输入任何内容.(如有提示是否覆盖,可输入y后按 ...
- 【bzoj3362-导航难题】带权并查集
题意: 约翰所在的乡村可以看做一个二维平面,其中有N 座牧场,每座牧场都有自己的坐标,编号为1到N.牧场间存在一些道路,每条道路道路连接两个不同的牧场,方向必定平行于X 轴或Y轴.连通两座牧场之间的路 ...
- 【BZOJ4720】【NOIP2016】换教室 [期望DP]
换教室 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description Input 第一行四个整数n,m,v ...
- 20155335俞昆《java程序设计》第三周总结
20155335 2006-2007-2 <Java程序设计>第三周学习总结 ## 教材学习内容总结 首先,关键是区基本类型和类类型,,产生对象必须定义类,类是一个概念,并不存在,对 ...
- 抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转: 就是将数字倒着存下来而已.(*^__^*) 嘻嘻…… 大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出. 详见代码. while (a) //将每位数字取出来, ...
- python urllib2练习发送简单post
import urllib2 import urllib url = 'http://localhost/1.php' while True: data = raw_input('(ctrl+c ex ...
- eclipse快捷键及eclipse一直building处理
1.输入Syso然后按 ALT+/ ------>System.out.println(); 2.CTRL+/ ------>添加或消除注释// 3.CTRL+SHIF ...
- ip和子网掩码的判断
只要记住B类IP的范围就好了(128以下的是A,128~191是B段,192以上是C段) 比如B类,网络地址为前两段,后面两段是主机地址,所以网络标识应该是255.255.0.0