zoukankan      html  css  js  c++  java
  • 1289 大鱼吃小鱼

    模拟,在这里用栈模拟,一开始用了结构体数组从首尾两端分别遍历果断超时,复杂度n*n,用栈来模拟是线性复杂度。

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<cmath>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long LL;
    #define MAXN 100001
    #define INF 0x3f3f3f3f
    int main()
    {
        LL n,temp,dir,ans;
        stack<LL> s;
        scanf("%lld",&n);
        ans = n;
        while(n--)
        {
            scanf("%lld%lld",&temp,&dir);
            if(dir==1)
                s.push(temp);
            else
            {
                while(!s.empty())
                {
                    if(s.top()<temp)
                    {
                        ans--;
                        s.pop();
                    }
                    else
                    {
                        ans--;
                        break;
                    }
                }
            }
        }
        printf("%lld
    ",ans);
        return 0;
    }
  • 相关阅读:
    (水题)洛谷
    (水题)洛谷
    洛谷
    (水题)洛谷
    POJ
    poj 3061(二分 or 尺取法)
    poj 2456(二分)
    poj 1064(二分答案)
    POJ 2559(单调栈)
    STL
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6253577.html
Copyright © 2011-2022 走看看