Gym - 101334F 单调栈

当时我的第一想法也是用单调栈,但是被我写炸了;我也不知道错在哪里;
看了大神的写法,用数组模拟的;
记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置。
当有数据要出栈的时候,说明栈里的数据已经不是最小了,右端点就是当前位置-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 单调栈的更多相关文章
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- Gym 100971D 单调栈
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- Gym 100971D Laying Cables 单调栈
Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- D - Laying Cables Gym - 100971D (单调栈)
题目链接:https://cn.vjudge.net/problem/Gym-100971D 题目大意:给你n个城市的信息,每一个城市的信息包括坐标和人数,然后让你找每一个城市的父亲,作为一个城市的父 ...
- Gym 100971D Laying Cables 二分 || 单调栈
要求找出每个a[i],找到离他最近而且权值比它大的点,若距离相同,输出权利最大的那个 我的做法有点复杂,时间也要500+ms,因为只要时间花在了map上. 具体思路是模拟一颗树的建立过程,对于权值最大 ...
- Gym - 102028H Can You Solve the Harder Problem? (后缀数组+RMQ+单调栈)
题意:求一个序列中本质不同的连续子序列的最大值之和. 由于要求“本质不同”,所以后缀数组就派上用场了,可以从小到大枚举每个后缀,对于每个sa[i],从sa[i]+ht[i]开始枚举(ht[0]=0), ...
- Gym - 101102D Rectangles (单调栈)
Given an R×C grid with each cell containing an integer, find the number of subrectangles in this gri ...
- BZOJ1767/Gym207383I CEOI2009 Harbingers 斜率优化、可持久化单调栈、二分
传送门--BZOJCH 传送门--VJ 注:本题在BZOJ上是权限题,在Gym里面也不能直接看,所以只能在VJ上交了-- 不难考虑到这是一个\(dp\). 设\(dep_x\)表示\(x\)在树上的带 ...
随机推荐
- django学习笔记——搭建博客网站
1. 配置环境,创建django工程 虚拟环境下建立Django工程,即创建一个包含python脚本文件和django配置文件的目录或者文件夹,其中manage.py是django的工程管理助手.(可 ...
- 【Tensorflow】 Object_detection之准备数据生成TFRecord
参考:Preparing Inputs 1.PASCAL VOC数据集 数据集介绍: PASCAL Visual Object Classes 是一个图像物体识别竞赛,用来从真实世界的图像中识别特定对 ...
- [转]ClassPath是什么
from: https://my.oschina.net/GivingOnenessDestiny/blog/603505 classpath 是什么classpath实际上就是编译后的 以 clas ...
- Grafana监控可视化环境搭建
依赖库Go 1.6NodeJS v4+sqlite3GO 环境搭建 vi /etc/profile export GOPATH="/root/go" export GOROOT=& ...
- innosetup卸载软件后,删除定时任务schedule task
代码如下: //innosetup自带的方法,当卸载软件时,根据卸载的状态改变时而触发 procedure CurUninstallStepChanged(CurUninstallStep: TUni ...
- pip使用的基本命令
基本的命令解释,如下图: 安装 sudo easy_install pip 列出已安装的包 pip freeze or pip list 导出requirements.txt pip freeze & ...
- BNU34067——Pair——————【找规律】
Pair Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name: M ...
- model操作涉及的所有字段(API)
一旦 数据模型 创建完毕, 自然会有存取数据的需要.本文档介绍了由 models 衍生而来的数据库抽象API,及如何创建,得到及更新对象. 贯穿本参考, 我们都会引用下面的民意测验(Poll)应用程序 ...
- [转]JS跨域解决方式 window.name
本文转自:http://www.cnblogs.com/lichuntian/p/4909465.html window.name 传输技术,原本是 Thomas Frank 用于解决 cookie ...
- JVM如何加载一个类的过程,双亲委派模型中有哪些方法
1.类加载过程:加载.验证.准备.解析.初始化 加载 在加载阶段,虚拟机主要完成三件事: 1.通过一个类的全限定名来获取定义此类的二进制字节流. 2.将这个字节流所代表的静态存储结构转化为方法 ...