Manthan, Codefest 17 B
来源:互联网 发布:telnet登录端口 编辑:程序博客网 时间:2024/06/01 10:12
B. Marvolo Gaunt’s Ring
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
树状数组区间求最值
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <vector>#include <string>using namespace std;int n;long long P[123456],Q[123456],R[123456];long long Y[123456];long long h[123456*3];long long k[123456*3];int lowbit(int x){ return (x&-x);}void update1(int x){ while(x<=n) { h[x]=P[x]; for(int i=1;i<lowbit(x);i<<=1) h[x]=max(h[x],h[x-i]); x+=lowbit(x); } return ;}void update2(int x){ while(x<=n) { k[x]=Y[x]; for(int i=1;i<lowbit(x);i<<=1) k[x]=max(k[x],k[x-i]); x+=lowbit(x); } return ;}long long find_max(int l,int r,long long a[],long long h[]){ long long ans=(long long)(-3*1e18-100000); while(r>=l){ ans=max(ans,a[r]); r--; for(;r-lowbit(r)>=l;r-=lowbit(r)){ ans=max(ans,h[r]); } } return ans;}int main(){ for(int i=1;i<=(int)(1e5+10);i++){ h[i]=(long long)(-3*1e18-100000); k[i]=(long long)(-3*1e18-100000); Y[i]=(long long)(-3*1e18-100000); } long long p,q,r; scanf("%d %I64d %I64d %I64d",&n,&p,&q,&r); for(int i=1;i<=n;i++){ long long a; scanf("%I64d",&a); P[i]=a*p; Q[i]=a*q; R[i]=a*r; } for(int i=1;i<=n;i++){ update1(i); } for(int i=1;i<=n;i++){ long long ma=find_max(1,i,P,h); Y[i]=ma+Q[i]; } for(int i=1;i<=n;i++){ update2(i); } long long Max=(long long)(-3*1e18-100000); for(int i=1;i<=n;i++){ long long ma=find_max(1,i,Y,k); Max=max(Max,find_max(1,i,Y,k)+R[i]); } cout<<Max<<endl; return 0;}
- Manthan, Codefest 17 B
- Manthan, Codefest 17 B-F题目详解
- Manthan, Codefest 17
- Manthan, Codefest 17
- Manthan, Codefest 17: B. Marvolo Gaunt's Ring
- codeforces Manthan, Codefest 17 B Marvolo Gaunt's Ring(dp)
- Manthan, Codefest 16 -B. A Trivial Problem
- codeforces Manthan, Codefest 17 B.Marvolo Gaunt's Ring (简单dp)
- Manthan, Codefest 17 B. Marvolo Gaunt's Ring(前后缀/dp)
- Manthan, Codefest 17 Marvolo Gaunt's Ring
- Manthan, Codefest 16 B. A Trivial Problem(数学)
- CodeForces Manthan, Codefest 16 B A Trivial Problem 二分
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup
- cf Manthan, Codefest 17 E. Salazar Slytherin's Locket
- Manthan, Codefest 17 E. Salazar Slytherin's Locket 数位DP
- Manthan, Codefest 17 C. Helga Hufflepuff's Cup
- Manthan Codefest 16 解题报告
- Manthan, Codefest 17 E. Salazar Slytherin's Locket(数位DP)
- Kotlin极简教程
- 结构体的地址与数据成员的地址
- erlang四大behaviour之二-gen_fsm
- React那些年, 踩过的一些坑(连载中...)
- samba服务器新增samba用户
- Manthan, Codefest 17 B
- python list ,set
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- spark中map和flatmap之间的区别
- 愿世界没有技术面全是Hr面
- 编程笔记
- 字符串作为数组下标
- Windows程序设计-位图和位块传输
- int main (int argc ,char *argv[])