题目:(题目传送门)

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
Input
5 1 2 3
1 2 3 4 5
Output
30
Input
5 1 2 -3
-1 -2 -3 -4 -5
Output
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.

题意

给你三个数p,q,r,给你一个数组,a1,a2,a3,a4 ai,aj,ak,…让你求pai+qaj+r*ak的最大值,并且有个要求,那就是
1 ≤ i ≤ j ≤ k ≤ n. ,也就是说不能在数组中随便找三个数进行相乘
我们要找最优的方案,类似于动态规划

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<vector>
#include<queue>
#include<map>
#define ll long long
using namespace std;
const int maxn=100023;
ll a[maxn];//存的是题目中所给数组
ll n;
ll pp;//
ll qq;
ll rr;
int main()
{int i,j;ll p,q,r,f,maxp,maxq,maxr;scanf("%lld%lld%lld%lld",&n,&p,&q,&r);for(i=0;i<n;i++){scanf("%lld",&a[i]);}if(p>0&&q>0&&r>0)//如果都是正数,都乘最大的呗{f=p+q+r;sort(a,a+n);printf("%lld\n",f*a[n-1]);}else if(p<=0&&q<=0&r<=0)//如果都是负数,都乘以最小的呗{f=p+q+r;sort(a,a+n);printf("%lld\n",f*a[0]);}else{pp=a[0]*p;qq=a[0]*q+pp;rr=a[0]*r+qq;for(i=1;i<n;i++){//pp表示从数组一开始到i,a[i]*p的最大值if(pp<a[i]*p)//如果pp小与此时的最新的,就变一下保留最优解{pp=a[i]*p;}else{pp=pp;///如果还是以前的大,就还用以前的呗}if(qq<pp+a[i]*q)//qq表示pp+a[i]*q{qq=pp+a[i]*q;}else{qq=qq;}if(rr<qq+a[i]*r)//rr表示qq+a[i]*r{rr=qq+a[i]*r;}else{rr=rr;}}printf("%lld\n",rr);//最后的rr就是最优解了,rr一直是p*ai+q*aj+r*ak的和}}

其实这样就保证了 1 ≤ i ≤ j ≤ k ≤ n.
还不明白欢迎评论区提问啊!!!

Marvolo Gaunt's Ring(类似于dp的做法)相关推荐

  1. Marvolo Gaunt's Ring ---CodeForces - 855B(思维题)

    题目链接:http://codeforces.com/problemset/problem/855/B Marvolo Gaunt's Ring Professor Dumbledore is hel ...

  2. B. Marvolo Gaunt’s Ring (递推)

    B. Marvolo Gaunt's Ring 题目链接 大致题意: 给你三个数 p,q,r,然后给你给你一个有序的序列,让你在序列中跳出三个数i,j,k(i <=j<=k)使得 p*a[ ...

  3. Codeforces 855B - Marvolo Gaunt's Ring

    855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个 ...

  4. Marvolo Gaunt's Ring 【CodeForces 855B】

    Marvolo Gaunt's Ring 求p * i + q * j + r * k(i<=j<=k)的最大值 虽然题中给的时间比较长但还是不可以用直接暴力用三次for循环,一定可以用一 ...

  5. Marvolo Gaunt's Ring CodeForces - 855B+线段树+维护区间最大值和最小值

    题目链接: Marvolo Gaunt's Ring CodeForces - 855B 题目大意: 给定一段序列:a1,a2,a3,--an, 给定三个数:p,q,r(注意数据范围,代码里ans=- ...

  6. CodeForces - 855B - Marvolo Gaunt's Ring(线段树 or DP)

    题目:CodeForces - 855B 题解: 1.用dp做的: dp[0][i]是前i个p*a[i]的最大值, dp[1][i]是在dp[0][i]的基础上加上q*a[i]的最大值,这样可以保证j ...

  7. codeforces 855-B. Marvolo Gaunt's Ring

    http://codeforces.com/problemset/problem/855/B 这个题一开始读错了没想到要按顺序之后看到这个就像枚举但是数据太大...emmm然后就有点蒙. 后来看了题解 ...

  8. 【ST】【CF855B】 Marvolo Gaunt's Ring

    传送门 Description 给定三个数 \(p~,~q~,~r~\),以及一个数组 \(a\), 找出三个数 \(i~,~j~,~k\) ,其中 \(i~\leq~j~\leq~k\) 最大化 \ ...

  9. codeforces 855-B. Marvolo Gaunt's Ring(背包问题)

    http://codeforces.com/problemset/problem/855/B 解题思路: 可以把p,q,r看成三个物品,当做背包问题处理. #include<iostream&g ...

最新文章

  1. 转换字符串中汉字为其拼音缩写(C#)
  2. BZOJ2588 Count on a tree 【树上主席树】
  3. 简单的MongoDB实践
  4. Mybatis接口注解开发
  5. JQuery选择器中的子元素选择器
  6. TV3是马来西亚第一家商营电视台
  7. java比赛_javamq
  8. 三维点云学习(4)5-ransac
  9. openstarck安装指南(图文详解,超小白版本)
  10. 数据挖掘:计算边的中介中心值 edge_betweenness value
  11. Java Balking模式
  12. OSEK间接网络管理(NM)
  13. 关于Altium Designer PCB元器件的3D封装
  14. Python -- 创建数字列表
  15. 金蝶迷你版凭证导入工具_金蝶kis迷你版如何插入凭证?
  16. MYSQL之STRAIGHT_JOIN
  17. 笛卡尔坐标系中八个卦限对应的位置
  18. spring的依赖注入 -------基于注解方式
  19. c语言计算机二级考试内容,2018年全国计算机二级C语言考试考什么内容
  20. 设计模式之模板方法模式(Template Method Pattern)

热门文章

  1. 基于小波变换的EMG信号病人数据matlab仿真分析
  2. 电脑中毒后常见的问题以及防毒的总结
  3. python中print函数的end和sep参数的用法
  4. 转载 loadrunner的一些问题解决
  5. Docker+Jenkins+Seneca构建去集中化微服务架构
  6. 高德地图 自动计算缩放_您应该了解的无服务器计算中的自动缩放模式
  7. a后缀名是什么格式文件,怎么打开.a文件
  8. 进入计算机管理界面win7,win7系统打开服务管理界面的操作方法
  9. STM32F103-LED模块
  10. 一阶系统开环传递函数表达式_古典控制理论(三)根轨迹法(闭环系统)