51Nod 1279 扔盘子

有一口井,井的高度为N,每隔1个单位它的宽度有变化。现在从井口往下面扔圆盘,如果圆盘的宽度大于井在某个高度的宽度,则圆盘被卡住(恰好等于的话会下去)。
盘子有几种命运:1、掉到井底。2、被卡住。3、落到别的盘子上方。
盘子的高度也是单位高度。给定井的宽度和每个盘子的宽度,求最终落到井内的盘子数量。
 
 
如图井和盘子信息如下:
井:5 6 4 3 6 2 3
盘子:2 3 5 2 4
 
最终有4个盘子落在井内。
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 <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) ((a,0,sizeof(a)))
typedef long long ll;
ll a[50006],dp[50006],n,m,x;
int main()
{
    scanf("%lld%lld",&n,&m);
    dp[n+1]=INF,dp[0]=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[n-i+1]);
        dp[n-i+1]=min(dp[n-i+2],a[n-i+1]);
    }
    int top=-1,cnt=0;
    for(int i=1;i<m;i++)
    {
        scanf("%d",&x);
        for(int j=top+1;j<=n;j++)
        {
            if(dp[j]>=x)
            {
                top=j;
                cnt++;
                break;
            }
            top=j;
        }
    }
    printf("%d\n",cnt);
    return 0;
}


猜你喜欢

转载自www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8973802.html