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

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

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

当有数据要出栈的时候,说明栈里的数据已经不是最小了,右端点就是当前位置-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. Vue添加新的响应式属性

    vm.userProfile = Object.assign({}, vm.userProfile, { age: , favoriteColor: 'Vue Green' })

  2. 【Tensorflow】 Object_detection之liunx服务器安装部署步骤记录

    环境:centos7+anaconda python3.6 步骤: 1.下载Models cd 到预存放目录下,执行: git clone https://github.com/tensorflow/ ...

  3. Java基础23-main方法

    /* 主函数(主方法) 1.public(访问修饰符,公共的)代表类或者该函数访问权限是最大的 2.static 代表主函数随着类的加载就已经存在了 3.void 代表主函数没有具体的返回值 4.ma ...

  4. Oracle 客户端、服务器、数据库、数据库对象(表、视图等)的关系

    1.数据库服务器 所谓数据库服务器,只是在机器上安装了一个数据库管理软件,这个软件可以管理多个数据库.一般开发人员会针对每一个应用创建一个数据库 2.单实例数据库模式下的数据库服务器.数据库.数据库实 ...

  5. 案例42-使用ajax获取crm中的客户列表

    1webcontent部分 1 修改menu.jsp代码 2 jsp/customer/list.jsp代码 <%@ page language="java" content ...

  6. H5禁止页面滑动/滚动

    禁止页面滚动--完美解决方案,滚动条显示与否,手持设备兼容与否 禁止页面滚动 有三种方法 1,依靠css 将页面 document.documentElement.style.overflow='hi ...

  7. 深入理解JavaScript系列(30):设计模式之外观模式

    介绍 外观模式(Facade)为子系统中的一组接口提供了一个一致的界面,此模块定义了一个高层接口,这个接口值得这一子系统更加容易使用. 正文 外观模式不仅简化类中的接口,而且对接口与调用者也进行了解耦 ...

  8. php 项目中自定义日志方法

    在现在项目中之前没有定义日志的方法,每次调试起来很麻烦,经常不能输出参数,只能用写日志的方法,一直用file_put_contents很烦躁,于是用了一点时间,写了这样一个方法: <?php / ...

  9. select, poll, epoll笔记

    看网络通信框架,netty, thrift,java nio等,最后都会通过select, poll, epoll或者socket等进行通信.查了些网页,总结一下.做个笔记 1. Socket单线程阻 ...

  10. 深入Java字符串

    深入Java字符串     Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,很多时候,我们对它既熟悉又陌生.   一.从根本上认识java.lang ...