首页 > 其他 > 详细

1289 大鱼吃小鱼

时间:2017-01-05 20:06:23      阅读:151      评论:0      收藏:0      [点我收藏+]

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

 

1289 大鱼吃小鱼

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!