Codeforces 264C Choosing Balls 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/CF264C.html
题目传送门 - CF264C
题意
给定一个有 $n$ 个元素的序列,序列的每一个元素是个球,第 $i$ 个球具有 $v_i$ 的值,颜色为 $c_i$ 。
一个序列的价值为每一个球价值和。
在一个序列中,第 $i$ 个球的价值为:
当 $c_i=c_{i-1}(i>1)$ 时,$value=a\times v_i$。
否则, $value=b\times v_i$ 。
接下来有 $q$ 组询问,每组询问给定 $a,b$ ,问在当前给定的 $a,b$ 下,原序列所有子序列(不一定要连续)的价值中的最大值。
$n\leq 10^5,q\leq 500$
题解
我们对于每一次询问分别处理。
首先我们考虑动态规划。
用 $dp[i][j]$ 表示前 $i$ 个元素中子序列以 颜色 $j$ 结尾的最大价值。
首先我们考虑每一个 $i$ 所代表的新球只会更新一个 $dp[i][j]$ ,所以我们可以把 $i$ 这一维省掉。
接下来我们考虑第 $i$ 个球的结果可能会从哪些情况继承:
1. 当前球为子序列第一个: $b\times v_i$
2. 从上一个和他颜色相同的球结尾的子序列继承:$dp[c_i]+a\times v_i$
3. 从和他颜色不同的球结尾的最大价值子序列继承:$dp[x]+b\times v_i$
现在最棘手的是如何找第 $3$ 种情况中的 $x$ 。
做法是:我们记录当前情况下 $dp$ 值最大和次大的颜色。
这两个中一定有一个是第三种情况需要的,所以就可以完成第三种情况了。
最后然后再拿当前球的结果更新 DP 数组的相应值即可。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=100005;
const LL INF=1LL<<56;
LL read(){
int x;
scanf("%d",&x);
return 1LL*x;
}
int n,q,c[N];
LL v[N],dp[N];
int main(){
scanf("%d%d",&n,&q);
for (int i=1;i<=n;i++)
v[i]=read();
for (int i=1;i<=n;i++)
scanf("%d",&c[i]);
while (q--){
LL a=read(),b=read();
LL ans=0;
int Max=0,Nxt=0;
for (int i=0;i<=n;i++)
dp[i]=-INF;
for (int i=1;i<=n;i++){
int color=c[i];
LL now=max(dp[color]+a*v[i],b*v[i]);
if (color!=Max)
now=max(now,dp[Max]+b*v[i]);
else/* if (color!=Nxt)*/
now=max(now,dp[Nxt]+b*v[i]);
if (now>dp[Max]){
if (Max!=color)
Nxt=Max,Max=color;
}
else if (now>dp[Nxt]&&color!=Max)
Nxt=color;
dp[color]=max(dp[color],now);
ans=max(ans,now);
}
printf("%I64d\n",ans);
}
return 0;
}
Codeforces 264C Choosing Balls 动态规划的更多相关文章
- poj 3783 Balls 动态规划 100层楼投鸡蛋问题
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098409.html 题目链接:poj 3783 Balls 动态规划 100层楼投鸡蛋问题 ...
- Codeforces 839C Journey - 树形动态规划 - 数学期望
There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...
- Codeforces 834D The Bakery - 动态规划 - 线段树
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...
- Codeforces 837D Round Subset - 动态规划 - 数论
Let's call the roundness of the number the number of zeros to which it ends. You have an array of n ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces 219D Choosing Capital for Treeland
http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- CodeForces 219D Choosing Capit
题目链接:http://codeforces.com/contest/219/problem/D 题目大意: 给定一个n个节点的数和连接n个节点的n - 1条有向边,现在要选定一个节点作为起始节点,从 ...
- CodeForces 623E Transforming Sequence 动态规划 倍增 多项式 FFT 组合数学
原文链接http://www.cnblogs.com/zhouzhendong/p/8848990.html 题目传送门 - CodeForces 623E 题意 给定$n,k$. 让你构造序列$a( ...
随机推荐
- pip的常用命令
前言 pip作为Python的御用包管理工具有着强大的功能,但是许多命令需要我们使用的时候借助搜索引擎查找(尤其是我), 于是我想将我使用到的命令整合下来,以后不用麻烦去找了,也希望能给你带来帮助.文 ...
- PYTHON- 操作系统和python程序
操作系统基础 应用程序的启动:(重点!!!) python解释器安装,多版本共存 执行python程序的两种方式 运行一个python程序经历的三个阶段(重要) python 的内存管理 ====== ...
- Vue-tab选项卡
<div id='test'> <ul class="nav" > <li v-for='(item,index) in dataNav' @clic ...
- 判断iOS版本号
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) { }
- nginx常用命令及简单配置
nginx常用命令 nginx -c /usr/local/nginx/conf/nginx.conf 启动nginx(windows下start nginx); nginx -s quit 停止ng ...
- 步步为营-87-imageAreaSelect插件使用(图片剪切)
1 引用文件 jquery.imgareaselect.min.cs imgareaselect-default.js 2 代码 <%@ Page Language="C#" ...
- Spring.Net 简单实例-01(IOC)
1.话不多说看操作.新建"Windows窗体应用程序" 2:通过配置文件创建IOC容器 首先引入安装包 3:定义一个接口(更好的体现封装性,当然也可以直接使用类) 定义一个类,实现 ...
- 没有-jackson相关依赖会抛出如下异常--------在spring官方文档有解释
<!--jackson相关依赖--><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackso ...
- nodejs 环境安装
参考网站 http://www.runoob.com/nodejs/nodejs-http-server.html https://github.com/nodesource/distribution ...
- 在IDEA中编写Spark的WordCount程序
1:spark shell仅在测试和验证我们的程序时使用的较多,在生产环境中,通常会在IDE中编制程序,然后打成jar包,然后提交到集群,最常用的是创建一个Maven项目,利用Maven来管理jar包 ...