Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)
Description
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.
Sample Input
Input
5 1 2 3
1 2 3 4 5
Output
30
Input
5 1 2 -3
-1 -2 -3 -4 -5
Output
12
Hint
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.
题意:找出最大的ai*p+aj*q+ak*r。保证i<=j<=k
解析:目前我在各个博客中找到了三种解法(其实思想差不多),首先是前后缀数组模拟的:用L[]来记录 i 左边的最大a[]*p值,R[]来记录 i 右边的最大a[]*r最大值,q为中间,最后遍历的时候直接q*a[]来更新最大值就好了:
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3f; //这注意了,1e18会WA,目前不知道原因
const int maxn = 1e5+;
ll a[maxn],L[maxn],R[maxn];
int main()
{
ll n,p,q,r;
cin>>n>>p>>q>>r;
for(int i=;i<=n;i++)
cin>>a[i];
L[]=a[]*p;
for(int i=;i<=n;i++)
L[i]=max(L[i-],a[i]*p);
R[n]=a[n]*r;
for(int i=n-;i>=;i--)
R[i]=max(R[i+],a[i]*r);
ll sum=-inf;
for(int i=;i<=n;i++)
sum=max(sum,L[i]+R[i]+q*a[i]);
cout<<sum<<endl;
}
第二种:也是模拟,代码最为简单
#include <iostream>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3f;
int main()
{
int n,p,q,r;
cin>>n>>p>>q>>r;
long long x;long long a=-inf,aa=-inf,aaa=-inf;
while(n--){
cin>>x;
a=max(a,p*x);
aa=max(aa,a+q*x);
aaa=max(aaa,aa+r*x);
}
cout<<aaa<<endl;
}
第三种:按背包问题来做
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long ll;
const int maxn=1e5+;
ll a[maxn],b[],dp[maxn][];
int main()
{
int n;
while(cin>>n)
{
for(int i=;i<=;i++)
cin>>b[i];
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
{
dp[i][]=;
for(int j=;j<=;j++)
dp[i][j]=-INF;
}
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
dp[i][j]=max(dp[i][j],max(dp[i-][j],dp[i][j-]+(ll)(a[i]*b[j])));
cout<<dp[n][]<<endl;
}
return ;
}
Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)的更多相关文章
- Codeforces 855B:Marvolo Gaunt's Ring(枚举,前后缀)
B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaun ...
- B. Marvolo Gaunt's Ring 前缀后缀
B. Marvolo Gaunt's Ring 这种一般只有三个的都可以处理前缀和后缀,再枚举中间这个值. 这个和之前写过的C. Four Segments 前缀后缀 处理方式很像. #include ...
- Codeforces 855B - Marvolo Gaunt's Ring
855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个 ...
- 【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\) 最大化 \ ...
- 一类巧妙利用利用失配树的序列DP
I.导入 求长度为\(\text{len}\)的包含给定连续子串\(\text{T}\)的 0/1 串的个数.(\(|T|<=15\)) 通常来说这种题目应该立刻联想到状压 DP 与取反集--这 ...
- POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)
题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件 ...
- 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame
http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...
- #415 Div2 Problem C Do you want a data? (math && 前后缀和 && 快速幂)
题意: 首先定义集合的F值为 这个集合里面最大值和最小值的差. 现给出一个拥有n个数的集合(没有相同的元素), 要求求出这个集合内所有子集的F的值的和.例如: {4.7}这个集合里面有子集{4}.{ ...
随机推荐
- kafka-console-consumer接收不到flume推送过来的消息
原因和解决方法:需要先启动kafka,再启动flume,两者启动有先后顺序.
- 伪奢侈品iPhone大降价,肉搏国产手机胜算几何?
据国外媒体报道,苹果在中国降低iPhone价格的策略已收到明显的效果,自从1月11日正式调整价格以来,iPhone在苏宁电器平台上的销量飙升83%,而天猫上的销量也增长了76%,其中最受欢迎的机型是i ...
- 基于zedboard的DMA设计笔记
2.BAR0空间的概念:BAR(Base Address Register ) 该组寄存器简称为BAR寄存器,BAR寄存器保存PCI设备使用的地址空间的基地址,该基地址保存的是该设备在PCI总线域中的 ...
- GoJS简单示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 机器学习(ML)十一之CNN各种模型
深度卷积神经网络(AlexNet) 在LeNet提出后的将近20年里,神经网络一度被其他机器学习方法超越,如支持向量机.虽然LeNet可以在早期的小数据集上取得好的成绩,但是在更大的真实数据集上的表现 ...
- IDEA 单行注释与代码对齐
效果 修改步骤 Settings -> Editor -> Code Style (1)修改.java文件的注释 comment 评论.注释.意见. (2)修改.html文件的注释 ( ...
- JavaScript 环境污染
定义全局变量有 3 种方式: 在任何函数体外直接使用 var 语句声明. var f = 'value1'; 直接添加属性到全局对象上.在 Web 浏览器中,全局作用域对象为 window. wind ...
- uni-app小程序组建
(1)新建组建:编辑器右击 新建组建 (2)传值 <template> <view class="myRankingList"> <block v-f ...
- c语言查漏补缺
getchar:执行getchar()函数时,首先从输入缓存区读取字符,直到输入缓存区为空时才等待从键盘继续输入.scanf()之间不要有printf操作. 逗号表达式 a= (++a,1,2),只取 ...
- JAVA学习笔记-数组的三种初始化方式
package Study; public class TestArray02 { public static void main(String[] args){//声明 int[] a; int ...