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( ...
随机推荐
- 【原创】大叔经验分享(36)CM部署kafka
1 下载kafka parcel http://archive.cloudera.com/kafka/parcels/latest/KAFKA-3.1.1-1.3.1.1.p0.2-el7.parce ...
- String类型作为方法的形参
代码: public class TestString { String str = new String("good"); char [] ch = {'a','b','c'}; ...
- python的学习笔记
1逻辑运算符不理解 2 在交互模式中,最后被输出的表达式结果被赋值给变量 _ .例如: >>> tax = 12.5 / 100 >>> price = 100.5 ...
- HttpListener通讯成功案例
1.创建WindowsService,如下代码 using System;using System.Net;using System.Net.Sockets;using System.ServiceP ...
- 整合 JIRA 和 Confluence 6
Jira 应用和 Confluence 可以完全的整合在一起.在 Confluence 中收集你项目组成员的想法,知识和计划.在 Jira 中跟踪你的系统出现的问题,让这 2 个应用同时工作. 了解更 ...
- 自执行匿名函数: (function() { /* code */ })();
1,常见格式:(function() { /* code */ })(); 2,解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括 ...
- ipone mac真机调试
safiri 识别不了iPhone 真机 需要在iPhone上 做设置 safri-> 高级 ->web检查器 进行设置,然后重新启动 safri即可...
- 部署描述符 web.xml
google的部署描述符详解: https://cloud.google.com/appengine/docs/flexible/java/configuring-the-web-xml-deploy ...
- cf1107d 映射关系
#include<bits/stdc++.h> using namespace std; ][]; int judge(int i){ ;j<=n;j++) ][j]); ; } i ...
- Nginx详解十:Nginx场景实践篇之Nginx静态资源场景配置
一.静态资源WEB服务 1.静态资源类型:非服务器动态运行生成的文件 2.静态资源服务场景-CDN 假设静态资源存储中心在云南,用户在北京去请求一个文件,那么就会造成一个传输的延时,而如果Nginx同 ...