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( ...
随机推荐
- 51nod--1265 四点共面 (计算几何基础, 点积, 叉积)
题目: 1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点是否在同一个平面内(4 ...
- [记录]一个有趣的url请求(nodejs)
1 前言 IDE是webstrom,跟项目编程语言,应该没有多大关系. 2 现象 两个看起来是一样的url,但是一个能访问一个不能访问. 然后,复制url到console中发现了差异,分别是:file ...
- C# 操作Excel加水印
首先下载免费版的Excel组件- Spire.XLS,安装完成后在bin目录里面有需要用到的dll文件,引用到自己项目里面. 我这里全引进来了,一共就四个: 界面 效果 全部代码 private st ...
- oracle存储大文本clob、blob
oracle存储大文本clob.blob 1 package cn.itcast.web.oracle.util; 2 3 import java.sql.Connection; 4 import j ...
- centos7 nginx图片 服务器可以访问ftp用户上传的图片资源的配置
注:本文参考了csdn:JAVA_DIRECTION的<nginx和ftp搭建图片服务器>一文.在实践中其文在centos7中还是存在缺陷性的 一:前提条件:是成功的安装好了ftp服务器和 ...
- pytorch的学习资源
安装:https://github.com/pytorch/pytorch 文档:http://pytorch.org/tutorials/beginner/blitz/tensor_tutorial ...
- nginx实践(二)之静态资源web服务(浏览器缓存场景)
配置语法-expires
- Java并发编程基础-ReentrantLock的机制
同步锁: 我们知道,锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁能够防止多个线程同时访问共享资源,在Lock接口出现之前,Java应用程序只能依靠synchronized关键字来实现同步锁 ...
- Max Sum (dp)
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...
- laravel 不理解的call方法
返回结果: 原来是调用同控制器的这四个方法之一...vendor\zhiyicx\plus-question\src\API2\Controllers\UserQuestionController.p ...