zoukankan      html  css  js  c++  java
  • 1279 扔盘子

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
    收藏
    关注
    有一口井,井的高度为N,每隔1个单位它的宽度有变化。现在从井口往下面扔圆盘,如果圆盘的宽度大于井在某个高度的宽度,则圆盘被卡住(恰好等于的话会下去)。
    盘子有几种命运:1、掉到井底。2、被卡住。3、落到别的盘子上方。
    盘子的高度也是单位高度。给定井的宽度和每个盘子的宽度,求最终落到井内的盘子数量。
     
     
    如图井和盘子信息如下:
    井:5 6 4 3 6 2 3
    盘子:2 3 5 2 4
     
    最终有4个盘子落在井内。
    本题由 @javaman 翻译。
    Input
    第1行:2个数N, M中间用空格分隔,N为井的深度,M为盘子的数量(1 <= N, M <= 50000)。
    第2 - N + 1行,每行1个数,对应井的宽度Wi(1 <= Wi <= 10^9)。
    第N + 2 - N + M + 1行,每行1个数,对应盘子的宽度Di(1 <= Di <= 10^9)
    Output
    输出最终落到井内的盘子数量。
    Input示例
    7 5
    5
    6
    4
    3
    6
    2
    3
    2
    3
    5
    2
    4
    Output示例
    4

    思路:利用单调栈

    #include<cstdio>
    #include<stack>
    #define MAXN 1e9
    #define min(a, b) (a)<(b)?(a):(b)
    using namespace std;

    stack<int> num;
    int main()
    {
     int n, m, t;
     int Min=MAXN;
     while (!num.empty())num.pop();
     scanf("%d%d", &n, &m);
     for (int i = 0; i < n; i++)
     {
      scanf("%d", &t);
      Min = min(t, Min);
      num.push(Min);
     }
     int ans = 0;
     for (int i = 0; i < m; i++)
     {
      scanf("%d", &t);
      if (num.empty())continue;
      int D = num.top();
      while (t>D&&!num.empty())
      {
       num.pop();
       if (num.empty())break;
       D = num.top();
      }
      if (t <= D&&!num.empty())
      {
       ans++;
       num.pop();
      }
     }
     printf("%d\n", ans);
    }



  • 相关阅读:
    电源锁
    Android的三种网络联接方式
    用tcpdump在手机上抓包
    图片出现波纹的问题
    Android 3.1以后 广播接收器的新机制
    OpenGL坐标
    用Messager进行IPC
    PHP 介绍
    View坐标,MotionEvent坐标, 二者的转换,可视区域
    OpenGL ES
  • 原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/9427366.html
Copyright © 2011-2022 走看看