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+10;
ll a[maxn],L[maxn],R[maxn];
int main()
{
    ll n,p,q,r;
    cin>>n>>p>>q>>r;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    L[1]=a[1]*p;
    for(int i=2;i<=n;i++)
        L[i]=max(L[i-1],a[i]*p);
    R[n]=a[n]*r;
    for(int i=n-1;i>=1;i--)
        R[i]=max(R[i+1],a[i]*r);
    ll sum=-inf;
    for(int i=1;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+10;
ll a[maxn],b[4],dp[maxn][4];
int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=1;i<=3;i++)
          cin>>b[i];
        for(int i=1;i<=n;i++)
          cin>>a[i];
        for(int i=0;i<=n;i++)
        {
            dp[i][0]=0;
            for(int j=1;j<=3;j++)
              dp[i][j]=-INF;
        }
        for(int i=1;i<=n;i++)
          for(int j=1;j<=3;j++)
            dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1]+(ll)(a[i]*b[j])));
        cout<<dp[n][3]<<endl;
    }
    return 0;
}

Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)

原文地址:https://www.cnblogs.com/liyexin/p/12339734.html

时间: 2024-10-09 16:05:04

Marvolo Gaunt's Ring(巧妙利用前后缀进行模拟)的相关文章

Codeforces 855B:Marvolo Gaunt&#39;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 sti

Codeforces 855B - Marvolo Gaunt&#39;s Ring

855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个值加起来的最大值. 代码: 代码①: #include<bits/stdc++.h> using namespace std; #define ll long long const int N=1e5+5; const int INF=0x3f3f3f3f; int a[N]; int premx

B Marvolo Gaunt&#39;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

#415 Div2 Problem C Do you want a data? (math &amp;&amp; 前后缀和 &amp;&amp; 快速幂)

题意: 首先定义集合的F值为  这个集合里面最大值和最小值的差. 现给出一个拥有n个数的集合(没有相同的元素), 要求求出这个集合内所有子集的F的值的和.例如: {4.7}这个集合里面有子集{4}.{7}.{4, 7}, 则这些子集的F值分别为4-4=0.7-7=0.7-4=3, 所以最后的结果就是0+0+3 = 3! 以下分析引用至 : http://blog.csdn.net/dragon60066/article/details/72599167 分析: 不难想到要先使数组升序方便计算和思

【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 [AC] 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace s

巧妙利用访问时间提取和重组代码的实践

说明 本文主要介绍巧妙利用访问时间提取和重组某嵌入式产品SDK代码的实践. 一  问题提出 目前产品平台为便于编译管理,要求各模块组织为include-source目录结构,分别存放头文件和源文件.但芯片厂家提供的SDK按功能划分为众多子目录,如subdir1(.c,.h)…subdirN(.c,.h)…Makefile,并将头文件和源文件一并存放在各子目录内. 此外,厂家SDK支持多种管理场景,通过选项开关编译不同的目录和文件.而产品硬件定板后管理场景固定,其他场景所涉及的代码将不再需要. 因

巧妙利用快速排序法的原理求一个数组中的第10大元素

//快速排序法 int QuickSort_process3(int *a, int low, int high) { int l, h, temp; l = low; h = high; temp = a[low]; while (l < h){ while (l< h&&a[h] >= temp) --h; if (l < h) a[l] = a[h]; while (l < h&&a[l] < temp) ++l; if (l &l

POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)

题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件的一组公共前后缀,那么易得pi=jq,如下图所示 若在字符串pi内,pk1与k2i为公共前后缀,有因为pi=jq所以对应的k2i在字符串jq内有后缀k4q与其等价.所以pi内的公共前后缀等也是pq的公共前后缀,而i=next[q],显然是个递归. 所以,可以通过不断使pos=next[pos]进行递

Hibernate给表和字段设置前后缀及分隔符

在<一口一口吃掉Hibernate(一)--使用SchemaExport生成数据表>中介绍了如何生成数据表.但是这只是最基本的.hibernate在生成或者操作数据库时,会受一些限制.比如一开始设计表的时候,直接写成了user(id,name,password,createTime)  这种格式的.但是客户后来要求表名称要以"t_"开头,字段名要以"stu_"开头,可以是一些数据库的关键字,单词之间以"_"隔开,方便他们维护.例如:T