51Nod 扔盘子

有一口井,井的高度为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 <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int MAXN = 50000+5;
const int INF = 1e9+100;
int W[MAXN], D[MAXN];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        W[n+1] = INF;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&W[n-i]);
            W[n-i] = min(W[n-i+1],W[n-i]);
        }
        for(int i=0; i<m; i++)
            scanf("%d",&D[i]);
        int ans = 0, tp = 1;
        for(int i=0; i<m; i++)
        {
            int tmp = lower_bound(W+tp, W+n+1, D[i])-W;
            if(tmp == n+1)
                break;
            tp = tmp+1;
            ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/bryce1010/p/9386976.html