有一口井,井的高度为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
思路:直观解释就是一个一个往里面填,但是如果井的宽度小于盘子的宽度,会卡住,如果直接暴力的话会超时,所以可以想办法;试想,先预处理一下井的宽度,使它的宽度按照由下往上升序来排列,
比如样例 井的宽度是 5 6 4 3 6 2 3
如果处理成 5 5 4 3 3 2 2
就可以直接往里面一个一个填
具体看代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#include<iostream>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=50005;
int w[N]={inf};
int main()
{
int m,n;
while(~scanf("%d %d",&n,&m))
{
int i,j,ans=0,e;
for(i=1;i<=n;i++)
{
scanf("%d",w+i);
if(w[i]>w[i-1])
w[i]=w[i-1];
}
// for(i=1;i<=n;i++)
// printf("%d ",w[i]);
// printf("\n");
int t=n;
for(i=1;i<=m;i++)
{
scanf("%d",&e);
for(;t>0;t--)
{
if(w[t]>=e)
{
t--;
ans++;
break;
}
}
}
printf("%d\n",ans);
}
return 0;
}