单调栈结构及应用
程序1:单调栈结构:
#include<iostream> //单调栈结构(自)
using namespace std;
#include<string>
#include<typeinfo>
#include<deque>
#include<algorithm>
#include<stack>
void getNearMore(int* arr,int len)
{
stack<int> sta;
for(int i=0;i<len;i++)
{
while(!sta.empty()&&arr[sta.top()]<arr[i])
{
cout<<arr[sta.top()]<<" 右边离得近且大的值为 "<<arr[i];
sta.pop();
if(sta.empty())
cout<<" 无左边离得近且大的值"<<endl;
else
cout<<" 左边离得近且大的值为 "<<arr[sta.top()]<<endl;
}
sta.push(i);
}
while(!sta.empty())
{
cout<<arr[sta.top()]<<" 无右边离得近且大的值";
sta.pop();
if(sta.empty())
cout<<" 无左边离得近且大的值";
else
cout<<" 左边离得近且大的值为 "<<arr[sta.top()]<<endl;
}
}
int main()
{
int a[]={5,4,3,6,7,2};
getNearMore(a,sizeof(a)/sizeof(int));
return 0;
}程序2:求最大子矩阵的大小
#include<iostream> //求最大子矩阵的大小
using namespace std;
#include<string>
#include<typeinfo>
#include<deque>
#include<algorithm>
#include<stack>
int maxRecFromBottom(int* height,int len)
{
if(height ==nullptr || len==0)
{
return 0;
}
stack<int> sta;
int maxArea = 0;
for(int i=0;i<len;i++)
{
while(!sta.empty()&&height[sta.top()]>=height[i])
{
int j = sta.top();
sta.pop();
int k = sta.empty()?-1:sta.top();
int curArea = (i-k-1)*height[j];
maxArea = max(maxArea,curArea);
}
sta.push(i);
}
while(!sta.empty())
{
int j = sta.top();
sta.pop();
int k = sta.empty()?-1:sta.top();
int curArea = (len - k -1)*height[j];
maxArea = max(maxArea,curArea);
}
return maxArea;
}
int maxRecSize(int* arr,int row,int clo)
{
if(arr==nullptr||row==0||clo==0)
{
return 0;
}
int maxArea = 0;
int height[clo]={0}; //需将数组中的数初始化为0,否则数组中的数为随机赋值,导致后面出错
for(int i =0;i<row;i++)
{
for(int j=0;j<clo;j++)
{
height[j]=*(arr+i*clo+j)==0?0:height[j]+1;
}
maxArea = max(maxArea,maxRecFromBottom(height,sizeof(height)/sizeof(int)));
}
return maxArea;
}
int main()
{
int a[][4]={1,0,1,1,1,1,1,1,1,1,1,0};
cout<<maxRecSize((int*)a, sizeof(a)/sizeof(a[0]), sizeof(a[0])/sizeof(int));
return 0;
}程序3:数组中的山峰对
#include<iostream> //数组中的山峰对
using namespace std;
#include<string>
#include<typeinfo>
#include<deque>
#include<algorithm>
#include<stack>
struct Pair
{
int value;
int times;
Pair(int val)
{
value = val;
times=1;
}
};
int nextIndex(int size,int i)
{
return i<(size-1)?(i+1):0;
}
int getInternalSum(int n)
{
return n==1?0:n*(n-1)/2;
}
int communications(int* arr,int size)
{
if(arr==nullptr||size < 2)
{
return 0;
}
int maxIndex=0;
for(int i=0;i<size;i++)
maxIndex=arr[maxIndex]<arr[i]?i:maxIndex;
int value=arr[maxIndex];
int index= nextIndex(size,maxIndex);
int res = 0;
stack<Pair> sta;
sta.push(Pair(value));
while(index!=maxIndex)
{
value=arr[index];
while(!sta.empty()&&sta.top().value<value)
{
int times = sta.top().times;
res+=getInternalSum(times) + 2*times;
sta.pop();
}
if(!sta.empty()&&sta.top().value==value)
{
sta.top().times++;
}
else
{
sta.push(Pair(value));
}
index = nextIndex(size,index);
}
while(!sta.empty())
{
int times = sta.top().times;
sta.pop();
res+= getInternalSum(times);
if(!sta.empty())
{
res+=times;
if(sta.size()>1)
{
res+=times;
}
else
{
res+=sta.top().times>1?times:0;
}
}
}
return res;
}
int main()
{
int a[]={5,4,4,3,5,4};
cout<<communications(a,sizeof(a)/sizeof(int));
return 0;
}
