单调栈结构及应用

程序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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-19 14:46
和女友两个人马上毕业,现在我在鹅实习995,周六日偶尔也去北京;她在北京金融007,经常忙到后半夜,周末也没啥休息机会两个人现在都不咋聊天了,一句话隔半小时甚至半天才回。&nbsp;她是个很优秀的妹子,工作也很努力,是值得学习一辈子的人。我在努力工作求转正,即便不行至少赚到了一段不错的实习经历。已经异地了半年,接下来可能还会持续是这个状态。我们都算是对方重要的人,只是感觉看上去不是很有未来的样子。希望牛友们给点的鼓励
梦旅奇缘:很难。异地首先就已经很难了,加上妹子是金融行业,忙碌高压,对情感需求很高,而且见惯纸醉金迷,你的很多优势在她那里可能就不算什么了。这种情况下,在她们那里遇到一个能及时照顾她的人,即使那人可能很多条件不如你,你也有可能被分手。 说白了,两个卷王就不太适合在一起。因为卷王最大的优势,在另一个卷王那里就不算优势了。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务