Marvolo Gaunt's Ring ---CodeForces - 855B(思维题)
题目链接:http://codeforces.com/problemset/problem/855/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 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.
看到这个题我的第一个想法是这么简单的题,直接暴力上呗,直接就WA了。。。这就是一个简单题嘛 woc
知道很久以后才发现这还是一个简单题,但是有坑,一定要留意 1 ≤ i ≤ j ≤ k ≤ n. 这个条件!!!
据说这个题有五六种做法,什么线段树啦 乱七八糟的,但是我不会。。。。只想到一个最简单的
下面的代码有两种做法,但是思想是完全一样的。只要注意了顺序,一切ojbk
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const ll inf=0x3f3f3f3f3f3f3f3f;
const ll mm=1e6+10;
ll a[mm];int main()
{ll n;ll pp[9];scanf("%lld",&n);for(ll i=1;i<=3;i++)scanf("%lld",&pp[i]);
//暴力死在了这里~~~
// for(ll i=1;i<=n;i++){
// scanf("%lld",&a[i]);
//
// x=max(x,pp[1]*a[i]);
// y=max(y,pp[2]*a[i]);
// z=max(z,pp[3]*a[i]);
// }
// res=x+y+z;
// printf("%lld",res); ll res=0;ll x=-inf,y=-inf,z=-inf;for(ll i=1;i<=n;i++){scanf("%lld",&a[i]);x=max(x,pp[1]*a[i]);y=max(y,x+pp[2]*a[i]);z=max(z,y+pp[3]*a[i]);}printf("%lld",z);return 0;
}//下面是另一种做法 dp
/*—————————————————————————ll dp[3][mm];//dp[i][j]表示前i个最大值之和
ll a[mm];
int main()
{ll p,q,r,n;cin>>n>>p>>q>>r;for(ll i=1;i<=n;i++)cin>>a[i];dp[0][0]=-inf;dp[1][0]=-inf;dp[2][0]=-inf;for(ll i=1;i<=n;i++){dp[0][i]=max(dp[0][i-1],p*a[i]);dp[1][i]=max(dp[1][i-1],dp[0][i]+q*a[i]);dp[2][i]=max(dp[2][i-1],dp[1][i]+r*a[i]);}cout<<dp[2][n]<<endl;return 0;
}
*/
Marvolo Gaunt's Ring ---CodeForces - 855B(思维题)相关推荐
- Marvolo Gaunt's Ring CodeForces - 855B+线段树+维护区间最大值和最小值
题目链接: Marvolo Gaunt's Ring CodeForces - 855B 题目大意: 给定一段序列:a1,a2,a3,--an, 给定三个数:p,q,r(注意数据范围,代码里ans=- ...
- Marvolo Gaunt's Ring 【CodeForces 855B】
Marvolo Gaunt's Ring 求p * i + q * j + r * k(i<=j<=k)的最大值 虽然题中给的时间比较长但还是不可以用直接暴力用三次for循环,一定可以用一 ...
- Codeforces 855B - Marvolo Gaunt's Ring
855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个 ...
- B. Marvolo Gaunt’s Ring (递推)
B. Marvolo Gaunt's Ring 题目链接 大致题意: 给你三个数 p,q,r,然后给你给你一个有序的序列,让你在序列中跳出三个数i,j,k(i <=j<=k)使得 p*a[ ...
- Marvolo Gaunt's Ring(类似于dp的做法)
题目:(题目传送门) Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he ...
- 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 ...
- CodeForces - 855B(思维)
真不知道自己在比赛的时候在干什么,这题都没写............. 邓布利多教授正在帮助哈利摧毁魂器.当他怀疑一个魂器出现在那里时,他去了冈特沙克.他看到Marvolo Gaunt的戒指,并将其确 ...
- codeforces 855-B. Marvolo Gaunt's Ring
http://codeforces.com/problemset/problem/855/B 这个题一开始读错了没想到要按顺序之后看到这个就像枚举但是数据太大...emmm然后就有点蒙. 后来看了题解 ...
- codeforces 855-B. Marvolo Gaunt's Ring(背包问题)
http://codeforces.com/problemset/problem/855/B 解题思路: 可以把p,q,r看成三个物品,当做背包问题处理. #include<iostream&g ...
最新文章
- 从空间数据库中删除所有拓扑对象
- WKWebView 那些坑
- 修改oracle+sga+size,oracle自动内存共享管理测试。修改 oracle 11g SGA_MAX_SIZE。
- 超详细Mysql的安装与卸载
- c vs java_c++ vs java---之一
- jQuery Object 和 HTML Element间的转换
- jetty java 禁用目录列表_java – 如何禁用Jetty的WebAppContext目录列表?
- 第 2-2 课:各种内部类和枚举类 + 面试题
- PowerDesigner(九)-模型文档编辑器(生成项目文档)
- 继涉黄被约谈 “比心陪练”App因内容涉宣扬暴力再被处罚
- IIS服务器应用程序不可用的解决办法
- 为什么你作为一个.NET的程序员工资那么低?
- 全文匹配方式查询(sql)
- 数据分析入门——深入浅出数据分析
- 小程序获取用户的openid(详解)
- python实现英文新闻摘要自动提取_Python实现英文新闻摘要自动提取
- 【IoT】创业:产品生产之老化测试
- 7z解压后重命名文件的方法
- Linux-京东字节百度提前批,一面二面都被问到了awk——实例篇(2)去重统计排序
- flutter源码下载(最新)