当时我的第一想法也是用单调栈,但是被我写炸了;我也不知道错在哪里;

看了大神的写法,用数组模拟的;

记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置。

当有数据要出栈的时候,说明栈里的数据已经不是最小了,右端点就是当前位置-1,那么就可以计算栈顶的元素所作的贡献;出栈完后,当前这个数字,他的最左边就是栈顶所能到达的位置;入栈;

#include <bits/stdc++.h>

using namespace std;

const int maxn =  + ;
int a[maxn];
int stacks[maxn];
long long sum[maxn];
int lef[maxn]; int main()
{
freopen("feelgood.in","r",stdin);
freopen("feelgood.out","w",stdout);
int n;
scanf("%d",&n); memset(sum,,sizeof(sum));
memset(lef,,sizeof(lef)); for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
sum[i] = sum[i-] + a[i];
} a[++n] = -;
int top = ;
long long ans = -;
int ansl = ,ansr = ;
for(int i=;i<=n;i++) {
if(top==||a[i]>a[stacks[top-]]) {
stacks[top++] = i;
lef[i] = i;
continue;
}
if(a[i]==a[stacks[top-]])
continue;
while(top>=&&a[i]<a[stacks[top-]]) {
top --;
long long tmp = (long long)a[stacks[top]]*(sum[i-]-sum[lef[stacks[top]]-]);
if(tmp>ans) {
ansr = i-;
ansl = lef[stacks[top]];
ans = tmp;
}
} lef[i] = lef[stacks[top]];
stacks[top++] = i; } printf("%lld\n%d %d\n",ans,ansl,ansr); return ;
}

(之前的错误找到了ans=-1,可以都为0)

 #include <bits/stdc++.h>

 using namespace std;

 int n;
const int maxn = +;
struct num {
long long value;
int maxleft,maxright;
int minleft,minright;
num():maxleft(),maxright(),minleft(),minright(){}
}a[maxn]; stack<pair<int,int> > S; long long sum[maxn]; void getMax()
{
while(!S.empty())
S.pop();
S.push(make_pair(a[].value,));
for(int i=;i<n;i++) {
while(!S.empty()&&S.top().first<=a[i].value) {
//int value = S.top().first;
int key = S.top().second;
S.pop(); a[i].maxleft +=a[key].maxleft;
if(!S.empty()) {
a[S.top().second].maxright +=a[key].maxright;
}
}
S.push(make_pair(a[i].value,i));
}
while(!S.empty()) {
int key = S.top().second;
S.pop();
if(!S.empty()) {
a[S.top().second].maxright +=a[key].maxright;
}
}
} void getMin()
{
while(!S.empty())
S.pop();
S.push(make_pair(a[].value,));
for(int i=;i<n;i++) {
while(!S.empty()&&S.top().first>=a[i].value) {
//int value = S.top().first;
int key = S.top().second;
S.pop(); a[i].minleft +=a[key].minleft;
if(!S.empty()) {
a[S.top().second].minright +=a[key].minright;
}
}
S.push(make_pair(a[i].value,i));
}
while(!S.empty()) {
int key = S.top().second;
S.pop();
if(!S.empty()) {
a[S.top().second].minright +=a[key].minright;
}
}
} int main()
{
freopen("feelgood.in","r",stdin);
freopen("feelgood.out","w",stdout);
scanf("%d",&n);
for(int i=;i<n;i++) {
scanf("%lld",&a[i].value);
sum[i+] = sum[i] + a[i].value;
}
// getMax();
getMin(); int l = ;
int r = ;
long long ans = -;
for(int i=;i<n;i++) {
long long tmp = a[i].value*(sum[i+a[i].minright]-sum[i-a[i].minleft+]);
if(ans<tmp) {
ans = tmp;
l = i - a[i].minleft + ;
r = i + a[i].minright;
}
} printf("%lld\n%d %d\n",ans,l,r); return ;
}

Gym - 101334F 单调栈的更多相关文章

  1. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  2. Gym 100971D 单调栈

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  3. Gym 100971D Laying Cables 单调栈

    Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...

  4. Code Forces Gym 100971D Laying Cables(单调栈)

    D - Laying Cables Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  5. D - Laying Cables Gym - 100971D (单调栈)

    题目链接:https://cn.vjudge.net/problem/Gym-100971D 题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...

  6. Gym 100971D Laying Cables 二分 || 单调栈

    要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...

  7. Gym - 102028H Can You Solve the Harder Problem? (后缀数组+RMQ+单调栈)

    题意:求一个序列中本质不同的连续子序列的最大值之和. 由于要求“本质不同”,所以后缀数组就派上用场了,可以从小到大枚举每个后缀,对于每个sa[i],从sa[i]+ht[i]开始枚举(ht[0]=0), ...

  8. Gym - 101102D Rectangles (单调栈)

    Given an R×C grid with each cell containing an integer, find the number of subrectangles in this gri ...

  9. BZOJ1767/Gym207383I CEOI2009 Harbingers 斜率优化、可持久化单调栈、二分

    传送门--BZOJCH 传送门--VJ 注:本题在BZOJ上是权限题,在Gym里面也不能直接看,所以只能在VJ上交了-- 不难考虑到这是一个\(dp\). 设\(dep_x\)表示\(x\)在树上的带 ...

随机推荐

  1. docker 安装使用gitlab

    官方镜像地址  ce版本: https://hub.docker.com/r/gitlab/gitlab-ce 文档地址: https://docs.gitlab.com/omnibus/docker ...

  2. 文献综述七:基于SSM的网上商城的开发与设计

    一.基本信息 标题:基于SSM的网上商城的开发与设计 时间:2018 出版源:Computer Knowledge and Technology 文件分类:对框架的研究 二.研究背景 为了解决现在电商 ...

  3. git 拉新项目

                   

  4. oracle 备份恢复篇(二)---rman 增备恢复--不完全恢复

    一,环境准备 全备脚本: export TMP=/tmp export TMPDIR=$TMP export ORACLE_BASE=/u01 export ORACLE_SID=prod expor ...

  5. SSH Intro - Remove entry

    ssh-keygen -f "/home/frank/.ssh/known_hosts" -R ec2-54-222-218-195.cn-north-1.compute.amaz ...

  6. 对象池3(方法功能)PoolManager(控制)PoolTimeObject(时间管理)text01(调用)Destorys(销毁)

    1.对象池PoolManager namespace kernal { public class PoolManager : MonoBehaviour { //“缓冲池”集合 public stat ...

  7. TOJ 2733 棋盘游戏

    Description 小 希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是 Gardon限制了只有某些格子才 ...

  8. ElasticSearch:集群(Cluster),节点(Node),分片(Shard),Indices(索引),replicas(备份)之间关系

    [Cluster]集群,一个ES集群由一个或多个节点(Node)组成,每个集群都有一个cluster name作为标识----------------------------------------- ...

  9. 如何给MySql创建连接用户并授权

    一般在为MySql创建用户时建议使用GRANT前台命令,当然如果对我们开发者而言,方法还有很多种,比如使用INSERT命令,甚至是直接修改mysql user数据表,但仍然建议按照MySQL规范去授权 ...

  10. Zoj 3870——Team Formation——————【技巧,规律】

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...