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}.{ ...
随机推荐
- Red_Hat yum源配置
http://www.linuxidc.com/Linux/2016-06/132171.htm
- threading 多线程
# coding:utf- import time from threading import Thread def foo(x):#这里可以带参数def foo(x) print "foo ...
- 084、Java数组之实现数组复制
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 修bug感悟
今天遇到了两个问题: 1. 使用 python 的 PIL 库读取照片的尺寸,对于有些图片,长宽和显示的效果相反,原因是照片中的 exif 有个属性 orientation,表示图片的翻转角度,详细说 ...
- Hyper-V网卡ping不通本机
可能的解决方案: 添加网卡时选择"旧版"
- Java图形与文本(18)
实例018 旋转图形 实例说明 本实例演示在Java中绘制图形时,如何对图形进行旋转.运行程序,单击窗体上的“顺时针”按钮,可以将图形顺时针旋转,效果如图1.18所示,用户还可以通过单击“逆时针”和 ...
- Servlet 3.0 新特性概述
Servlet 3.0 新特性概述 Servlet 3.0 作为 Java EE 6 规范体系中一员,随着 Java EE 6 规范一起发布.该版本在前一版本(Servlet 2.5)的基础上提供了若 ...
- keras人工神经网络构建入门
//2019.07.29-301.Keras 是提供一些高度可用神经网络框架的 Python API ,能帮助你快速的构建和训练自己的深度学习模型,它的后端是 TensorFlow 或者 Theano ...
- 获取网卡MAC、硬盘序列号、CPU_ID、BIOS编号
抄来的 获取网卡MAC.硬盘序列号.CPU ID.BIOS编号 本文中所有原理及思想均取自网络,有修改.其中获取硬盘序列号.获取CPU编号.获取BIOS编号的原始代码的著作权归各自作者所有. 以下代码 ...
- 022、MySQL字符串的拼接
SELECT CONCAT('曾经沧海难为水-','-除却巫山不是云') #字符串拼接 SELECT CONCAT('AB','CD','EF'); #ABCDEF #字符串拼接 SELECT CON ...