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}.{ ...
随机推荐
- PDO 小知识
一.前言 PDO(PHP Data Object)提供了一个通用接口访问多种数据库,即抽象的数据模型支持连接多种数据库. PDO扩展为PHP定义了一个访问数据库的轻量.持久的接口.其本身并不能实现任何 ...
- 【LOJ2540】「PKUWC2018」随机算法
题意 题面 给一个 \(n\) 个点 \(m\) 条边的无向图.考虑如下求独立集的随机算法:随机一个排列并按顺序加点.如果当前点能加入独立集就加入,否则不加入.求该算法能求出最大独立集的概率. \(n ...
- 连续(Continuity) - 有界(Bounded) - 收敛(Convergence)
连续(Continuity) 所有点连续 -> 一致连续 (uniform continuity) -> 绝对连续 -> 李普希兹连续(Lipschitz) 弱 ...
- P1091 N-自守数
1091 N-自守数 (15分) 如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3,而 2 的末尾两位正好是 9,所以 9 是一个 3-自守 ...
- MySQL设置各类字符集
一.查看字符集编码: 登录mysql show variables like '%character%'; 二.修改编码: 编辑/etc/my.cnf ,设置后的配置文件如下: [root@node0 ...
- java实现下划线转驼峰
废话少说,直接上代码 import java.util.regex.Matcher; import java.util.regex.Pattern; public class Temp { publi ...
- VTK基于MFC单文档的开发
目录 项目的搭建 相关头文件的引用 添加成员变量vtkRenderer, vtkMFCWindow CXxxView()中实例化变量vtkRenderer CXxxView::OnInitialUpd ...
- 014.CI4框架CodeIgniter数据库操作之:查询数据库,并让数据以对象的方式返回查询结果
01. 我们在CI4框架中的Model文件夹新建一个User_model.php的文件,使用的是getResultArray,表示并让数据以数组的方式返回查询结果,代码如下: <?php nam ...
- 004.Oracle数据库 , 查询多字段连接合并
/*Oracle数据库查询日期在两者之间*/ SELECT PKID , OCCUR_DATE, PKID || TO_CHAR( OCCUR_DATE, ' yyyy/mm/dd hh24:mi:s ...
- java开发之分页查询
工具类 package com.luer.comm.utils; import java.util.List; public class PageBean<T> { //已知数据 priv ...