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\n",ans);
    return 0;
}

 

posted @ 2017-01-05 18:51  joeylee97  阅读(126)  评论(0编辑  收藏  举报