菜鸡单调栈

登录—专业IT笔试面试备考平台_牛客网

https://ac.nowcoder.com/acm/contest/881/A

自己菜的一批,居然有既然能数目不一样的,以后就算在找也是一定有前面那一段是不合法的
用单调栈会快很多,怎么解释呢,看代码吧。
AC代码:
#include <bits/stdc++.h>
#include<stack>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int s1[N],s2[N];
int ans;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
   s1[0]=0;
   s2[0]=0;
    while(cin>>n){
        stack<int> st1;
        stack<int> st2;
        for(int i=1;i<=n;i++)
            cin>>s1[i];
        for(int i=1;i<=n;i++)
            cin>>s2[i];
        st1.push(s1[1]);
        st2.push(s2[1]);
        for(int i=2;i<=n;i++)
        {
            while(!st1.empty()&&st1.top()>s1[i]){
                st1.pop();
            }
            st1.push(s1[i]);
            while(!st2.empty()&&st2.top()>s2[i]){
                st2.pop();
            }
            st2.push(s2[i]);
            if(st1.size()!=st2.size())
            {
                cout<<i-1<<endl;
                break;
            }
            if(i==n)
                cout<<n<<endl;
        }
    }
    return 0;
}

全部评论

相关推荐

ResourceUtilization:算法很难了,现在都需要相关论文还有对应的实习,可以先试试中厂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务