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 ...
随机推荐
- python3学习笔记
之前一直使用python2.7,最近打算学习下python3教程,再此记录下一些要点(未完待续...) 1.缩进 缩进有利有弊.好处是强迫你写出格式化的代码,但没有规定缩进是几个空格还是Tab.按照约 ...
- JS-函数作用域
如果变量在函数内没有声明(没有使用 var 关键字),该变量为全局变量.
- LeetCode--028--实现strStr() (java)
实现 strStr() 函数. 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在,则返 ...
- ElasticSearch改造研报查询实践
背景: 1,系统简介:通过人工解读研报然后获取并录入研报分类及摘要等信息,系统通过摘要等信息来获得该研报的URI 2,现有实现:老系统使用MSSQL存储摘要等信息,并将不同的关键字分解为不同字段来提供 ...
- win2012R2 的IIS报错HTTP404,报错在计算机上找不到服务W3SVC等等
一.背景 今天远程给客户解决IIS的默认网页浏览找不到文件夹,报错HTTP404,找了很多原因,而且也报错在计算机上找不到服务W3SVC等等,如图所示: 二.原因 试了很多方法都不可以重装IIS都不行 ...
- 用python代码模拟登录网站
方法一:直接使用已知的cookie访问 特点: 简单,但需要先在浏览器登录 具体步骤: 1.用浏览器登录,获取浏览器里的cookie字符串 先使用浏览器登录.再打开开发者工具,转到network选项卡 ...
- 小程序input组件获得焦点时placeholder内容有重影
这个问题是小程序input组件的bug,目前的解决办法可以,在input标签上加一个其他标签,显示placeholder内容,获得焦点时消失,失去焦点时候再让其显示 <view class='i ...
- 思科模拟器PacketTracer7--利用一台交换机将两台pc划分到不同vlan下
实验2—3 实验内容:将同一交换机下的两台pc划分到不同vlan中 实验工具:思科模拟器PacketTracer7 使用设备:一台交换机,两台PC 实验步骤: 一.配置网络拓扑图 注:1.连线可选择闪 ...
- vmware三种网络模式的工作原理及配置详解
vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NAT(网络地址转换模式).Host-Only(仅主机模式). 打开vmware虚拟机,我们可以在选项栏的“编辑”下的 ...
- ASP.NET MVC Display Mode 移动端视图 配置对微信内置浏览器的识别
最近在捣鼓一个稍微有点low的商城网站,没有计划做app却要求有个wap版,而前端又没有做成响应式,时间WTF,直接利用了asp.net mvc的Display Mode Provider. 使用方式 ...