51nod1102(单调栈/预处理)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102
题意:中文题诶~
思路:单调栈/预处理 (这篇博客就不细写了啦,只给出代码和转过来的两篇不错的题解,好困了~)
单调栈:http://blog.csdn.net/u012773338/article/details/40265223
代码:
#include <iostream>
#include <stack>
#define ll long long
#define MAXN 50010
using namespace std; ll a[MAXN], ans=;
stack<int> st; int main(void){
ios::sync_with_stdio(false), cin.tie(), cout.tie();
int n;
cin >> n;
for(int i=; i<n; i++){
cin >> a[i];
}
a[n]=-;
for(int i=; i<=n; i++){
if(st.empty()||a[i]>=a[st.top()]){
st.push(i);
}else if(a[i]<a[st.top()]){
int cnt;
while(!st.empty()&&a[i]<a[st.top()]){
ans=max(ans, a[st.top()]*(i-st.top()));
cnt=st.top();
st.pop();
}
st.push(cnt);
a[cnt]=a[i];
}
}
cout << ans << endl;
return ;
}
预处理:http://blog.csdn.net/queuelovestack/article/details/52326276
代码:
#include <iostream>
#define ll long long
#define MAXN 50010
using namespace std; ll l[MAXN], r[MAXN], a[MAXN]; int main(void){
ios::sync_with_stdio(false), cin.tie(), cout.tie();
int n;
cin >> n;
for(int i=; i<=n; i++){
cin >> a[i];
}
for(int i=; i<=n; i++){
int k=i-;
while(a[i]<=a[k]){
k=l[k]-;
}
l[i]=k+;
}
for(int i=n; i>; i--){
int k=i+;
while(a[i]<=a[k]){
k=r[k]+;
}
r[i]=k-;
}
ll ans=;
for(int i=; i<=n; i++){
ans=max((r[i]-l[i]+)*a[i], ans);
}
cout << ans << endl;
}
51nod1102(单调栈/预处理)的更多相关文章
- [51nod1102]面积最大的矩形(单调栈||预处理)
题意:求序列上某区间最小值乘区间长度的最大值. 解题关键:很早就在<挑战程序设计竞赛>中见过了,单调栈模板题,注意弹栈时如何处理后面的元素. 法一:单调栈 #include<bits ...
- BZOJ4540 Hnoi2016 序列 【莫队+RMQ+单调栈预处理】*
BZOJ4540 Hnoi2016 序列 Description 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- ...
- 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 ...
- bzoj 4826: [Hnoi2017]影魔 [主席树 单调栈]
4826: [Hnoi2017]影魔 题意:一个排列,点对\((i,j)\),\(p=max(i+1,j-1)\),若\(p<a_i,a_j\)贡献p1,若\(p\)在\(a_1,a_2\)之间 ...
- BZOJ.4540.[HNOI2016]序列(莫队/前缀和/线段树 单调栈 RMQ)
BZOJ 洛谷 ST表的一二维顺序一定要改过来. 改了就rank1了哈哈哈哈.自带小常数没办法. \(Description\) 给定长为\(n\)的序列\(A_i\).\(q\)次询问,每次给定\( ...
- bzoj 4540 [HNOI 2016] 序列 - 莫队算法 - Sparse-Table - 单调栈
题目传送门 传送点I 传送点II 题目大意 给定一个长度为$n$的序列.询问区间$[l, r]$的所有不同的子序列的最小值的和. 这里的子序列是连续的.两个子序列不同当且仅当它们的左端点或右端点不同. ...
- 【CF815D】Karen and Cards 单调栈+扫描线
[CF815D]Karen and Cards 题意:一张卡片有三个属性a,b,c,其上限分别为A,B,C,现在有n张卡片,定义一张卡片能打败另一张卡片当且仅当它的至少两项属性要严格大于另一张的对应属 ...
- 【AtCoder】ARC067 F - Yakiniku Restaurants 单调栈+矩阵差分
[题目]F - Yakiniku Restaurants [题意]给定n和m,有n个饭店和m张票,给出Ai表示从饭店i到i+1的距离,给出矩阵B(i,j)表示在第i家饭店使用票j的收益,求任选起点和终 ...
随机推荐
- bind(),live(),delegate(),on()绑定事件方式
1.bind():向匹配元素添加一个或多个事件处理器. 适用所有版本,但是自从jquery1.7版本以后bind()函数推荐用on()来代替. $(selector).bind(event,data, ...
- STS、Eclipse报java.lang.OutOfMemoryError: PermGen space 内存溢出
我使用的工具是STS, Eclipse同理: 打开如下界面: 左则选择项目启动使用的Tomcat-->在右侧面板Tab项中选择" Arguments":在VM argumen ...
- Linux (ubuntu和redhat) 常用命令及细节
1.关闭防火墙(Ubuntu) sudo ufw disable 2.vi 拷贝 参考http://blog.sina.com.cn/s/blog_601331150100ecfr.html 一) ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)
题目链接:http://codeforces.com/contest/828 A. Restaurant Tables time limit per test 1 second memory limi ...
- malloc和new的区别是什么?
http://zhidao.baidu.com/link?url=iUDUZeJtj1o12PvUETLlJgvAMqzky5HxGCJRGnULpsO8HdWAdjKkQqGCJ9-o-aTu8NP ...
- ES搜索排序,文档相关度评分介绍——Field-length norm
Field-length norm How long is the field? The shorter the field, the higher the weight. If a term app ...
- Linux_服务器_06_VMware虚拟机下安装CentOS7.0图文教程
二.参考资料 1.VMware虚拟机下安装CentOS7.0图文教程
- Eclipse_插件_03_反编译插件_Eclipse Class Decompiler
一.插件优势 此插件比jd-eclipse更加强大,反编译之后不会像jd-eclipse一样出现注释符号. 二.插件下载地址 1.github https://github.com/cnfree/Ec ...
- ACM学习历程——HDU2227 Find the nondecreasing subsequences(线段树 && dp)
Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., ...
- 【Lintcode】105.Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...