当前位置: 首页 > news >正文

做 直销网站 公司吗/上海网站外包

做 直销网站 公司吗,上海网站外包,用文字写美食个人网站设计作品,宜兴市住房和城乡建设局网站什么是单调栈? 单调栈,顾名思义,就是具有单调性的栈。它依旧是⼀个栈结构,只不过⾥⾯存储的数据是递增或者递减的。这种结构是很容易实现的(如下⾯的代码),但重点是维护⼀个单调栈的意义是什么 …
  1. 什么是单调栈?
    单调栈,顾名思义,就是具有单调性的栈。它依旧是⼀个栈结构,只不过⾥⾯存储的数据是递增或者递减的。这种结构是很容易实现的(如下⾯的代码),但重点是维护⼀个单调栈的意义是什么
#include <iostream>  
#include <stack>  
using namespace std;  
const int N = 3e6 + 10;  
int a[N], n;  
void test1()  
{  stack<int> st; // 维护⼀个单调递增的栈  for(int i = 1; i <= n; i++)  {  // 栈⾥⾯⼤于等于 a[i] 的元素全部出栈  while(st.size() && st.top() >= a[i]) st.pop();  st.push(a[i]);  }  
}  
void test2()  
{  stack<int> st; // 维护⼀个单调递减的栈  for(int i = 1; i <= n; i++)  {  // 栈⾥⾯⼩于等于 a[i] 的元素全部出栈  while(st.size() && st.top() <= a[i]) st.pop();  st.push(a[i]);  }  
}
  1. 单调栈解决的问题
    单调栈能帮助我们解决以下四个问题:
  • 寻找当前元素左侧,离它最近,并且⽐它⼤的元素在哪;
  • 寻找当前元素左侧,离它最近,并且⽐它⼩的元素在哪;
  • 寻找当前元素右侧,离它最近,并且⽐它⼤的元素在哪;
  • 寻找当前元素右侧,离它最近,并且⽐它⼩的元素在哪。
    虽然是四个问题,但是原理是⼀致的。因此,只要解决⼀个,举⼀反三就可以解决剩下的⼏个
  1. 寻找当前元素左侧,离它最近,并且⽐它⼤的元素在哪
    从左往右遍历元素,构造⼀个单调递减的栈。插⼊当前位置的元素的时:
  • 如果栈为空,则左侧不存在⽐当前元素⼤的元素;
  • 如果栈⾮空,插⼊当前位置元素时的栈顶元素就是所找的元素。
    注意,因为我们要找的是最终结果的位置。因此,栈⾥⾯存的是每个元素的下标
输⼊:  
9 1  
4 10 6 3 3 15 21 8  
输出:  
0 0 0 3 4 4 0 0 8
#include <iostream>  
#include <stack>  
using namespace std;  
const int N = 3e6 + 10;  
int a[N], n;  
int ret[N];  
void test()  
{  stack<int> st; // 维护⼀个单调递减的栈for(int i = 1; i <= n; i++)  {  // 栈⾥⾯⼩于等于 a[i] 的元素全部出栈  while(st.size() && a[st.top()] <= a[i]) st.pop();  // 此时栈顶元素存在,栈顶元素就是所求结果  if(st.size()) ret[i] = st.top();  st.push(i); // 存的是下标  }  for(int i = 1; i <= n; i++)  {  cout << ret[i] << " ";  }  cout << endl;  
}  int main()  
{  cin >> n;  for(int i = 1; i <= n; i++) cin >> a[i];  test(); cout << endl;  return 0;  
}
  1. 寻找当前元素左侧,离它最近,并且⽐它⼩的元素在哪
    从左往右遍历元素,构造⼀个单调递增的栈。插⼊当前位置的元素的时:
  • 如果栈为空,则左侧不存在⽐当前元素⼩的元素;
  • 如果栈⾮空,插⼊当前位置元素时的栈顶元素就是所找的元素。
    注意,因为我们要找的是最终结果的位置。因此,栈⾥⾯存的是每个元素的下标
输⼊:  
9 1  
4 10 6 3 3 15 21 8
输出:  
0 1 2 2 1 1 6 7 6
#include <iostream>  
#include <stack>  
using namespace std;  
const int N = 3e6 + 10;  
int a[N], n;  
int ret[N];  
void test()  
{  stack<int> st; // 维护⼀个单调递增的栈  for(int i = 1; i <= n; i++)  {  // 栈⾥⾯⼤于等于 a[i] 的元素全部出栈  while(st.size() && a[st.top()] >= a[i]) st.pop();  // 此时栈顶元素存在,栈顶元素就是所求结果  if(st.size()) ret[i] = st.top();  st.push(i); // 存的是下标  }  for(int i = 1; i <= n; i++)  {  cout << ret[i] << " ";  }  cout << endl;  
}  int main()  
{  cin >> n;  for(int i = 1; i <= n; i++) cin >> a[i];  test(); cout << endl;return 0;  
}

针对其余两种情况,我们仅需逆序遍历数组即可。
5. 寻找当前元素右侧,离它最近,并且⽐它⼤的元素在哪
从右往左遍历元素,构造⼀个单调递减的栈。插⼊当前位置的元素的时:

  • 如果栈为空,则左侧不存在⽐当前元素⼤的元素;
  • 如果栈⾮空,插⼊当前位置元素时的栈顶元素就是所找的元素。
    注意,因为我们要找的是最终结果的位置。因此,栈⾥⾯存的是每个元素的下标
输⼊:  
9 1  
4 10 6 3 3 15 21 8  
输出:  
2 3 7 7 7 7 8 0 0
#include <iostream>  
#include <stack>  
using namespace std;  
const int N = 3e6 + 10;  
int a[N], n;  
int ret[N];  
void test()  
{  stack<int> st; // 维护⼀个单调递减的栈for(int i = n; i >= 1; i--)  {  // 栈⾥⾯⼩于等于 a[i] 的元素全部出栈  while(st.size() && a[st.top()] <= a[i]) st.pop();  // 此时栈顶元素存在,栈顶元素就是所求结果  if(st.size()) ret[i] = st.top();  st.push(i); // 存的是下标  }  for(int i = 1; i <= n; i++)  {  cout << ret[i] << " ";  }  cout << endl;  
}  
int main()  
{  cin >> n;  for(int i = 1; i <= n; i++) cin >> a[i];  test(); cout << endl;  return 0;  
}
  1. 寻找当前元素右侧,离它最近,并且⽐它⼩的元素在哪
    从右往左遍历元素,构造⼀个单调递增的栈。插⼊当前位置的元素的时:
  • 如果栈为空,则左侧不存在⽐当前元素⼩的元素;
  • 如果栈⾮空,插⼊当前位置元素时的栈顶元素就是所找的元素。
    注意,因为我们要找的是最终结果的位置。因此,栈⾥⾯存的是每个元素的下标
输⼊:  
9 1  
4 10 6 3 3 15 21 8
输出:  
0 5 4 5 0 0 9 9 0
#include <iostream>  
#include <stack>  
using namespace std;  
const int N = 3e6 + 10;  
int a[N], n;  
int ret[N];  
void test()  
{  stack<int> st; // 维护⼀个单调递增的栈  for(int i = n; i >= 1; i--)  {  // 栈⾥⾯⼤于等于 a[i] 的元素全部出栈  while(st.size() && a[st.top()] >= a[i]) st.pop();  // 此时栈顶元素存在,栈顶元素就是所求结果  if(st.size()) ret[i] = st.top();  st.push(i); // 存的是下标  }  for(int i = 1; i <= n; i++)  {  cout << ret[i] << " ";  }  cout << endl;  
}  int main()  
{  cin >> n;  for(int i = 1; i <= n; i++) cin >> a[i];  test(); cout << endl;return 0;  
}
  • 找左侧,正遍历;找右侧,逆遍历;
  • ⽐它⼤,单调减;⽐它⼩,单调增。
P5788 【模板】单调栈 - 洛谷

右侧离它最近并且⽐它⼤的元素:

  • 逆序遍历数组;
  • 构造⼀个单调递减的栈;
  • 进栈时,栈顶元素就是最终结果
#include <bits/stdc++.h>
using namespace std;const int N = 3e6 + 10;int n;
int a[N];
int ret[N];int main()
{ios::sync_with_stdio(false);cin.tie(0);cin >> n;for (int i = 1; i <= n; i++) cin >> a[i];stack<int> st;for (int i = n; i >= 1; i--){while (st.size() && a[st.top()] <= a[i]) st.pop();if (st.size()) ret[i] = st.top();st.push(i);}for (int i = 1; i <= n; i++) cout << ret[i] << " ";cout << endl;return 0;
}
P1901 发射站 - 洛谷
#include <bits/stdc++.h>
using namespace std;typedef long long LL;const int N = 1e6 + 10;int n;
LL h[N], v[N];
LL sum[N];int main()
{ios::sync_with_stdio(false);cin.tie(0);cin >> n;for (int i = 1; i <= n; i++) cin >> h[i] >> v[i];//找左边stack<int> st;for (int i = 1; i <= n; i++){//递减栈while (st.size() && h[st.top()] <= h[i]) st.pop();if (st.size()){sum[st.top()] += v[i];}st.push(i);}//找右边while (st.size()) st.pop(); //清空for (int i = n; i >= 1; i--){//递减栈while (st.size() && h[st.top()] <= h[i]) st.pop();if (st.size()){sum[st.top()] += v[i];}st.push(i);}LL ret = 0;for (int i = 1; i <= n; i++) ret = max(ret, sum[i]);cout << ret << endl;return 0;
}
SP1805 HISTOGRA - Largest Rectangle in a Histogram - 洛谷

![[Pasted image 20250408143205.png]]

对于x位置⼦矩阵,找到左侧离它最近并且⽐它⼩的位置y ,那么[x+1, y]之间就是该矩阵能到达的左端。
同理再找到右侧离它最近并且⽐它⼩的位置z ,那么[y, z - 1]之间就是该矩阵能到达的右端。
对于每⼀个⼦矩阵,求出它向左以及向右能延伸的最⼤⻓度即可

#include <bits/stdc++.h>
using namespace std;typedef long long LL;const int N = 1e5 + 10;int n;
LL h[N];
LL x[N], y[N];int main()
{ios::sync_with_stdio(false);cin.tie(0);while (cin >> n, n){for (int i = 1; i <= n; i++) cin >> h[i];//找左边stack<int> st;for (int i = 1; i <= n; i++){//单调递增栈while (st.size() && h[st.top()] >= h[i]) st.pop();if (st.size()) x[i] = st.top();else x[i] = 0;st.push(i);}//找右边while (st.size()) st.pop();for (int i = n; i >= 1; i--){//单调递增栈while (st.size() && h[st.top()] >= h[i]) st.pop();if (st.size()) y[i] = st.top();else y[i] = n + 1;st.push(i);}LL ret = 0;for (int i = 1; i <= n; i++){ret = max(ret, h[i] * (y[i] - x[i] - 1));        }cout << ret << endl;}return 0;
}
http://www.whsansanxincailiao.cn/news/31981728.html

相关文章:

  • 国内漂亮网站欣赏/青岛快速排名
  • 网站怎么做拉新/网推平台有哪些比较好
  • 网站的基本建设投资/他达拉非片多少钱一盒
  • h5网站页面/深圳做网站
  • 网站建设百度云/营销网站制作公司
  • 网站开发技术包括/推广网站公司
  • 什么平台可以做网站推广/一个产品的市场营销策划方案
  • 企业网站建设参考文献/情感式软文广告
  • jsp动态网站开发技术/刷百度指数
  • 手机网站制作案例/云计算培训费用多少钱
  • 做外贸的人如何上国外网站/搜索引擎营销的优势
  • 网站建设报价费用是多少/国内新闻大事20条
  • 官方网站举例/网络营销推广的总结
  • 建筑bim工程网报入口/徐州网页关键词优化
  • 开业时网站可以做哪些活动吗/百度关键词怎么做排名
  • 什么平台可以做网站推广/优化网站页面
  • 东莞企业营销型网站/制作一个网站需要多少费用
  • 青海企业网站开发定制/seo服务工程
  • 我国网站建设的不足/长沙网站seo收费标准
  • 物价工作信息网站建设/2024年疫情还会封控吗
  • 做网站在/惠州seo外包费用
  • 国外过期域名查询网站/重庆网站搭建
  • 东坑网站建设公司/怎么样做一个自己的网站
  • 宁夏建设厅网站官网/排名优化方案
  • 餐饮加盟网站建设方案/免费国外ddos网站
  • 分类信息网站推广的意义/网推软件有哪些
  • 福州公司建站模板/新疆头条今日头条新闻
  • 2022年最火的网页游戏/抖音seo系统
  • 免费做网页的网站/优化排名工具
  • 万链网站做的怎么样/活动营销案例100例