The Preliminary Contest for ICPC China Nanchang National Invitational I题
Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval.
Now she is planning to find the max value of the intervals in her array. Can you help her?
Input
First line contains an integer n(1 \le n \le 5 \times 10 ^5n(1≤n≤5×105).
Second line contains nn integers represent the array a (-10^5 \le a_i \le 10^5)a(−105≤ai≤105).
Output
One line contains an integer represent the answer of the array.
样例输入复制
5
1 2 3 4 5
样例输出复制
36 题意:给你一个长度为n的序列,让你找一段区间使得该段 区间和 和 它的区间最小值 乘积最大,并输出最大值。 思路:因为我们不知道当最小值是多少时 以它为最小值的区间 的和 与 它的乘积最大,所以我们只能枚举每个位置,以每个位置为最小值,求出以它为最小值的最大值 当我们以当前位置的值作为最小值时,我们怎么求出以它为最小值的最大区间和呢
首先,我们要知道以当前值为区间最小值时,它可能的区间范围

以上图为例 ,当我们以第三个位置的值为最小值时,它的区间范围为(2,5)
我们可以在区间(2,5)内找到一段区间和最大的区间(一定要包含第三号位置),就找到了以第三个位置为最小值的题目要找的最大值。
找这段和最大的区间时我们可以用前缀和建线段树来求出 当当前位置值为正数时,我们从当前位置的左边的区间找出最小的前缀和,从当前位置的右边区间找出最大的前缀和,那么,用右边找到的值减去左边的值就是我们满足我们要求的最大的区间和
(这样用右边找到的前缀和减去左边找到的前缀和不就是在中间我们要找的包含当前位置的区间最大和吗,负数的时候相反) 代码:
#include<cstdio>
#include<algorithm>
#include<stack>
#define ll long long
using namespace std;
struct point{
ll a;//值
ll x;//位置
}s[];
stack<point> st;
struct{
ll l,r;//记录以当前位置为最小值区可以取区间的范围
}bd[];
ll sum[];//前缀和
struct{
ll mx,mn;//记录前缀和的最大值和最小值
}tree[];
void build(ll l,ll r,ll k){
if(l==r){
tree[k].mn=tree[k].mx=sum[l];
return ;
}
ll mid=(l+r)>>;
build(l,mid,k*);
build(mid+,r,k*+);
tree[k].mx=max(tree[k*].mx,tree[k*+].mx);
tree[k].mn=min(tree[k*].mn,tree[k*+].mn);
return ;
}
ll query1(ll l,ll r,ll k,ll L,ll R){//找最大值
//printf("WW%d %d %d %d\n",l,r,L,R);
if(l>=L&&r<=R){
return tree[k].mx;
}
ll a,b;
a=-1e18;
b=-1e18;
ll mid=(l+r)>>;
if(L<=mid)
a=query1(l,mid,k*,L,R);
if(R>mid)
b=query1(mid+,r,k*+,L,R);
return max(a,b);
}
ll query2(ll l,ll r,ll k,ll L,ll R){//找最小值
if(l>=L&&r<=R){
return tree[k].mn;
} //printf("EE%d %d %d %d\n",l,r,L,R);
ll a,b;
a=1e18;
b=1e18;
ll mid=(l+r)>>;
if(L<=mid)
a=query2(l,mid,k*,L,R);
if(R>mid)
b=query2(mid+,r,k*+,L,R);
return min(a,b);
}
int main(){
ll n;
scanf("%lld",&n);
sum[]=;
for(ll i=;i<=n;i++){
scanf("%lld",&s[i].a);
s[i].x=i;
sum[i]=sum[i-]+s[i].a;
}
for(ll i=;i<=n;i++){//单调栈求范围
if(st.empty()){//如果当前栈为空,当前要放入位置的左极限位置就是自己的位置
bd[s[i].x].l=i;
st.push(s[i]);
}
else{
point tp=st.top();
if(tp.a==s[i].a){//如果当前位置的值与栈顶的值一样,那么当前要放入位置的左极限就和栈顶位置的左极限相同
bd[s[i].x].l=bd[tp.x].l;
st.push(s[i]);
}
else if(tp.a<s[i].a){//如果当前放入位置的值大于栈顶位置的值,那么当前放入位置的左极限位置就是自己的位置
bd[s[i].x].l=i;
st.push(s[i]);
}
else{//如果当前放入位置的值小于栈顶位置的值
st.pop();
ll r=tp.x;
ll l=bd[tp.x].l;
bd[tp.x].r=r;//栈顶位置的右极限就是当前栈顶位置的位置
if(!st.empty())
tp=st.top();
while(!st.empty()&&tp.a>s[i].a){//如果下一个栈顶位置的值还是大于当前位置
st.pop();
bd[tp.x].r=r;//后面的栈顶可以到的右极限都和第一次栈顶位置的右极限相同,因为栈是从小到上递增的
l=bd[tp.x].l;//更新要放入的当前位置可以到的左极限
if(!st.empty())
tp=st.top();
}
bd[s[i].x].l=l;
st.push(s[i]);
}
}
}
if(!st.empty()){//更新
point tp=st.top();
ll r=tp.x;
while(!st.empty()){
tp=st.top();
st.pop();
bd[tp.x].r=r;
}
}
// for(ll i=1;i<=n;i++){
// printf("%d %d %d\n",i,bd[i].l,bd[i].r);
// }
build(,n,);
ll ans=-1e18;
// printf("OK");
for(ll i=;i<=n;i++){
ll r1,l1;
if(s[i].a>){
r1=query1(,n,,s[i].x,bd[s[i].x].r);
l1=query2(,n,,bd[s[i].x].l-,s[i].x-);
//printf("%lld %lld\n",l1,r1);
ans=max((r1-l1)*s[i].a,ans);
}
else if(s[i].a<){
r1=query2(,n,,s[i].x,bd[s[i].x].r);
l1=query1(,n,,bd[s[i].x].l-,s[i].x-);
//printf("%lld %lld\n",l1,r1);
ans=max((r1-l1)*s[i].a,ans);
}
else
ans=max(0LL,ans);
}
printf("%lld\n",ans);
}
The Preliminary Contest for ICPC China Nanchang National Invitational I题的更多相关文章
- 2019The Preliminary Contest for ICPC China Nanchang National Invitational
The Preliminary Contest for ICPC China Nanchang National Invitational 题目一览表 考察知识点 I. Max answer 单调栈+ ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- The Preliminary Contest for ICPC China Nanchang National Invitational
目录 Contest Info Solutions A. PERFECT NUMBER PROBLEM D. Match Stick Game G. tsy's number H. Coloring ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
- The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest
打网络赛 比赛前的准备工作要做好 确保 c++/java/python的编译器能用 打好模板,放在桌面 A. PERFECT NUMBER PROBLEM #include <cstdio> ...
- Max answer(The Preliminary Contest for ICPC China Nanchang National Invitational)
Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I.Max answer单调栈
题面 题意:一个5e5的数组,定义一个区间的值为 这个区间的和*这个区间的最小值,注意数组值有负数有正数,求所有区间中最大的值 题解:如果全是正数,那就是原题 POJ2796 单调栈做一下就ok 我们 ...
- 2019 The Preliminary Contest for ICPC China Nanchang National Invitational(A 、H 、I 、K 、M)
A. PERFECT NUMBER PROBLEM 题目链接:https://nanti.jisuanke.com/t/38220 题意: 输出前五个完美数 分析: 签到.直接百度完美数输出即可 #i ...
- 计蒜客 38228. Max answer-线段树维护单调栈(The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer 南昌邀请赛网络赛) 2019ICPC南昌邀请赛网络赛
Max answer Alice has a magic array. She suggests that the value of a interval is equal to the sum of ...
随机推荐
- java8新的时间日期库及使用示例
转自:https://www.cnblogs.com/comeboo/p/5378922.html 来自:Java译站 链接:http://it.deepinmind.com/java/2015/03 ...
- 解决IE11安装时需要“获取更新”(IE11离线安装)
方法一:说明:目前是针对Windows7 64位操作系统安装! 1. 在C盘下新建文件夹,取名为“IE11”. 2. 将官网下载的IE11离线包放到此文件夹中. 3. win + r 打开运行窗口,输 ...
- vuex的学习和理解
初识Vuex: vuex是 vue官方推荐的一个状态管理器,也是vue专用的一个插件.当我们遇到很多状态改变时,组件之间的通信就会变得复杂,这时候vuex的强大就体现出来了. Vuex 应用的核心就是 ...
- web安全测试--sql注入攻击
先要自行了解sql的几个概念: 1. or '1'='1' 2. order by 3. union : 联合查询需要表字段相同 sql注入攻击漏洞判断步骤: 1.‘ 2.查看数据库信息 3.绕过过 ...
- 使用POST请求实现页面的跳转
项目情景: 当用户选择几个item之后,点击 查看 按钮之后, 页面跳转到展示items详情页面. 实现: 如果可以使用get请求, 直接在前端使用windows.loaction.href = &q ...
- 采用Tensorflow内部函数直接对模型进行冻结
# enhance_raw.py # transform from single frame into multi-frame enhanced single raw from __future__ ...
- asp.net 跨域访问
摘自 https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests- ...
- PHP设置凌晨时间戳
这种需求应是很常见的,但一直没有时间整理. 一天可以领取2次奖励,今天领完了那就等明天再来. 这里面涉及到一个很重要的一点就是凌晨12点的时间戳,以前一直在前端去做判断.最近发现在后端用PHP获取凌晨 ...
- ubuntu 系统开机执行脚本设置
在ubuntu 系统中常常有一些操作需要开机时手动去执行,有一些固定的脚本文件可以通过改写启动项脚本让系统启动时自动执行 方法: 编辑/etc/下的rc.local脚本,把对应的需要执行的脚本写在ex ...
- Freeswitch 各版本一键安装脚本 Freeswitch 快速安装 G729编解码库
最近有时间整理,写了freeswitch的一键安装包分享一下,里面带有 mysql=0 是否选择安装mysql. 其他提示:脚本里面集成了安装g729脚本,支持录音.转码的bcg729脚本. 1.下载 ...