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( ...
随机推荐
- python bytes/str
http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3/
- C++ DLL
DLL(Dynamic Link Library)(1)DLL 的编制与具体的编程语言及编译器无关只要遵循约定的DLL接口规范和调用方式,用各种语言编写的DLL都可以相互调用.譬如Windows提供的 ...
- [转]Fiddler模拟post四种请求数据
1 前言 仅作为记录使用. 2 内容 post请求主体详解: 对于get请求来说没有请求主体entity-body.对于post请求而言,不会对发送请求的数据格式进行限制,理论上你可以发任意数据,但是 ...
- Nginx(./configure --help)
# ./configure --help --help print this message --prefix=PATH set installation prefix --sbin-path=PAT ...
- Oracle Database 11g : SQL 基础
简介 1:课程目标 2:课程 目标 3:Oracle Database 11g 以及相关产品概览 1:Oracle Database 11g :重点领域 2:Oracle Fusion Middlew ...
- 本文转自 MyEclipse 2015反编译插件安装
本文转自MyEclipse 2015反编译插件安装 分享一下下载插件的地址,百度网盘:链接:http://pan.baidu.com/s/1nturiAH 密码:yk73 其次:我来说下具体操作步骤: ...
- java-HTML&javaSkcript&CSS&jQuery&ajax
CSS 伪装 1.<style>a;link{color:#000000} a:visited{color:#000000; a.:hover{color:#FF00FF} a:acti ...
- Vue中使用Vue.component定义两个全局组件,用单标签应用组件时,只显示一个组件的问题和 $emit的使用。
解决方法: 定义了两个 Vue.component 在 el 中使用的时候要用 双标签, 用单表标签的时候,只会显示第个 组件间 这样写只显示 welcome-button 组件 <welcom ...
- AI学习吧-结算中心
结算中心流程 在结算中心中,主要是对用户添加到购物车商品的结算,由于用户可能添加了多个课程,但是,结算时会选择性的进行支付.在结算时会选中课程id,和对应的价格策略.在后台,首先会对用户进行校验,验证 ...
- python函数之各种器
一: 装饰器 1:装饰器模板 def wrapper(func): def inner(*args,**kwargs): ret =func(*args,**kwargs) return ret re ...