Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)
B. Marvolo Gaunt's Ring
Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is still affected by its curse. Professor Snape is helping Dumbledore remove the curse. For this, he wants to give Dumbledore exactly x drops of the potion he made.
Value of x is calculated as maximum of p·ai + q·aj + r·ak for given p, q, r and array a1, a2, ... an such that 1 ≤ i ≤ j ≤ k ≤ n. Help Snape find the value of x. Do note that the value of x may be negative.
Input
First line of input contains 4 integers n, p, q, r ( - 109 ≤ p, q, r ≤ 109, 1 ≤ n ≤ 105).
Next line of input contains n space separated integers a1, a2, ... an ( - 109 ≤ ai ≤ 109).
Output
Output a single integer the maximum value of p·ai + q·aj + r·ak that can be obtained provided 1 ≤ i ≤ j ≤ k ≤ n.
Examples
5 1 2 3
1 2 3 4 5
30
5 1 2 -3
-1 -2 -3 -4 -5
12
Note
In the first sample case, we can take i = j = k = 5, thus making the answer as 1·5 + 2·5 + 3·5 = 30.
In second sample case, selecting i = j = 1 and k = 5 gives the answer 12.
题意
给出一个有n个数的序列a[1],a[2]……a[n],使得在i<=j<=k的条件下,令p*a[i]+q*a[j]+r*a[k]的值最大并输出这个值
思路
维护一个位置的前后缀left和right,left[i]表示位置i之前的元素与p相乘的最大值,right表示位置i之后的元素与q相乘的最大值,然后枚举每个位置,计算max(left[i]+q*a[i]+right([i])
代码
1 #include <bits/stdc++.h>
2 #define ll long long
3 #define ull unsigned long long
4 #define ms(a,b) memset(a,b,sizeof(a))
5 const int inf=0x3f3f3f3f;
6 const ll INF=0x3f3f3f3f3f3f3f3f;
7 const int maxn=1e6+10;
8 const int mod=1e9+7;
9 const int maxm=1e3+10;
10 using namespace std;
11 ll Left[maxn];
12 ll Right[maxn];
13 ll a[maxn];
14 int main(int argc, char const *argv[])
15 {
16 #ifndef ONLINE_JUDGE
17 freopen("/home/wzy/in.txt", "r", stdin);
18 freopen("/home/wzy/out.txt", "w", stdout);
19 srand((unsigned int)time(NULL));
20 #endif
21 ios::sync_with_stdio(false);
22 cin.tie(0);
23 int n;
24 ll p,q,r;
25 cin>>n>>p>>q>>r;
26 for(int i=0;i<n;i++)
27 cin>>a[i];
28 Left[0]=a[0]*p;
29 Right[n-1]=a[n-1]*r;
30 for(int i=1;i<n;i++)
31 Left[i]=max(Left[i-1],p*a[i]);
32 for(int i=n-2;i>=0;i--)
33 Right[i]=max(Right[i+1],r*a[i]);
34 ll ans=-INF;
35 for(int i=0;i<n;i++)
36 ans=max(ans,Left[i]+q*a[i]+Right[i]);
37 cout<<ans<<endl;
38 #ifndef ONLINE_JUDGE
39 cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
40 #endif
41 return 0;
42 }
Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)的更多相关文章
- Codeforces 855B - Marvolo Gaunt's Ring
855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个 ...
- Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)
Description Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as h ...
- B. Marvolo Gaunt's Ring 前缀后缀
B. Marvolo Gaunt's Ring 这种一般只有三个的都可以处理前缀和后缀,再枚举中间这个值. 这个和之前写过的C. Four Segments 前缀后缀 处理方式很像. #include ...
- Codeforces 587D - Duff in Mafia(2-SAT+前后缀优化建图)
Codeforces 题面传送门 & 洛谷题面传送门 2-SAT hot tea. 首先一眼二分答案,我们二分答案 \(mid\),那么问题转化为,是否存在一个所有边权都 \(\le mid\ ...
- 【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring
[链接]h在这里写链接 [题意] 给你n个数字; 让你在其中找出三个数字i,j,k(i<=j<=k); 使得p*a[i]+q*a[j]+r*a[k]最大; [题解] /* 有一个要 ...
- 【ST】【CF855B】 Marvolo Gaunt's Ring
传送门 Description 给定三个数 \(p~,~q~,~r~\),以及一个数组 \(a\), 找出三个数 \(i~,~j~,~k\) ,其中 \(i~\leq~j~\leq~k\) 最大化 \ ...
- CodeForces - 855B ring 前缀和
邓布利多教授正在帮助哈利摧毁魂器.当他怀疑一个魂器出现在那里时,他去了冈特沙克.他看到Marvolo Gaunt的戒指,并将其确定为魂器.虽然他摧毁了它,但仍然受到诅咒的影响.斯内普教授正在帮助邓布利 ...
- codeforces 579D D. "Or" Game(前后缀+贪心)
题目链接: D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- HDU 4763 Theme Section(KMP+枚举公共前后缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...
随机推荐
- 大数据学习day25------spark08-----1. 读取数据库的形式创建DataFrame 2. Parquet格式的数据源 3. Orc格式的数据源 4.spark_sql整合hive 5.在IDEA中编写spark程序(用来操作hive) 6. SQL风格和DSL风格以及RDD的形式计算连续登陆三天的用户
1. 读取数据库的形式创建DataFrame DataFrameFromJDBC object DataFrameFromJDBC { def main(args: Array[String]): U ...
- 08-认证(Authorization)
这又是一个非常实用的功能,对我们做接口测试来说,经常要处理登录认证的情况 .如果不用这个Authorization其实也能解决认证的问题,无非就是把要认证的数据按照要求在指定位置传入参数即可.比如我们 ...
- Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- 【编程思想】【设计模式】【其他模式】graph_search
Python版 https://github.com/faif/python-patterns/blob/master/other/graph_search.py #!/usr/bin/env pyt ...
- Orcale 数据加载
CSV 逗号分隔值格式文件 1,若要加载的文件不是CSV格式,可以修改数据文件,用分隔符来替换逗号:也可以修改控制文件,将FIELDS TERMINATED BY的值改为实际的分隔符. eg, 要向s ...
- 【Java基础】ExecutorService的使用
ExecutorService是java中的一个异步执行的框架,通过使用ExecutorService可以方便的创建多线程执行环境. 本文将会详细的讲解ExecutorService的具体使用. 创建 ...
- centos 7 zookeeper 单体和集群搭建
1.操作相关命令 1.0 安装命令 wget :下载解压包 tar -xzvf :解压 1.1 创建节点 create / node : 创建一个名字为node的 空节点 creat ...
- PL/SQL实例1
declare --定义游标 cursor cemp is select to_char(hiredate,'yyyy') from emp; phiredate varchar2(4); ...
- 【Word】自动化参考文献-交叉引用
第一步:设置参考文献标号 开始-定义新编号格式中,定义参考文献式的方框编号: 这里注意不要把他原来的数字去掉 第二步:选择交叉引用 插入-交叉引用: 第三步:更新标号 如果更新标号,使用右键-更新域. ...
- springboot项目启动慢,怀疑jdk有问题
项目启动慢,并且没有启动日志,开发环境和windows服务器都正常,到linux后出现问题,你觉得会是哪儿的问题? 最近收到一位同事求助,说springboot应用在客户的一台Linux机器上启动非常 ...