1289 大鱼吃小鱼

时间:2017-01-05 20:06:23   收藏:0   阅读:153

模拟,在这里用栈模拟,一开始用了结构体数组从首尾两端分别遍历果断超时,复杂度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;
}

 

原文:http://www.cnblogs.com/joeylee97/p/6253577.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!