• 51nod 1289 大鱼吃小鱼


    题目来源: Codility
    基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
     收藏
     关注
    有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右。游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼。从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右)。问足够长的时间之后,能剩下多少条鱼?
    Input
    第1行:1个数N,表示鱼的数量(1 <= N <= 100000)。
    第2 - N + 1行:每行两个数A[i], B[i],中间用空格分隔,分别表示鱼的大小及游动的方向(1 <= A[i] <= 10^9,B[i] = 0 或 1,0表示向左,1表示向右)。
    Output
    输出1个数,表示最终剩下的鱼的数量。
    Input示例
    5
    4 0
    3 1
    2 0
    1 0
    5 0
    Output示例
    2


    题意:中文题

    思路:类似括号的匹配,栈的应用题

    #include <iostream>
    #include<cstdio>
    #include<stack>
    using namespace std;
    const int maxn=1e5+10;
    typedef struct
    {
        int body;
        int dir;
    }fish;
    fish f[maxn];
    stack<fish>s;
    int main()
    {
        int n;
        scanf("%d",&n);
        int cnt=0;
        int cnt1=n;
        int maxbody=0;
        int flag=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&f[i].body,&f[i].dir);
            if(f[i].dir==0&&s.empty()==1)
            {
                cnt++;
            }
           else if(f[i].dir==1)
           {
               s.push(f[i]);
               maxbody=max(maxbody,f[i].body);
               cnt++;
           }
           else
           {
               flag=1;
               while(s.empty()==0&&flag==1)
               {
    
    
               if(f[i].body>s.top().body)
               {
                   cnt1--;
                   flag=1;
                   s.pop();
               }
               else
               {
                   cnt1--;
                   flag=0;
               }
               }
           }
        }
        cout<<cnt1<<endl;
        return 0;
    }
    


  • 相关阅读:
    webstorm打开项目找不到git
    Redis 下载与安装(Windows版)
    element-UI el-table样式(去边框和滚动条样式)
    MVVM
    HTTP
    TCP/IP
    vue双向绑定
    Vue组件化原理
    JavaScript
    css中可继承与不可继承属性
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387323.html
Copyright © 2020-2023  润新知