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

输出最终落到井内的盘子数量。

Sample Input

7 5
5
6
4
3
6
2
3
2
3
5
2
4

Sample Output

4

题解:

由于下面的井口能进多宽的盘子取决于它及上面井口宽度的最小值,所以我们可以每次读进来一个井口宽度都

与上一个比较一下,如果大与上一个那么久赋值为上一个井口的宽度。接下来在按升序排序,每次拿进来一个盘子就

二分找到第一个宽度大于等于它的井口t,然后更新二分区间的左边界为t+1,重复到没有为止就行了。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>

using namespace std;

const int MAXN = 50005;

int J[MAXN];
int P[MAXN];

int main(){
	
	int N,M;
	int ans;
	J[0] = 0x3f3f3f3f;
	while(scanf("%d %d",&N,&M)!=EOF){
		ans = 0;
		for(int i=1 ; i<=N ; i++){
			scanf("%d",&J[i]);
			if(J[i]>J[i-1])J[i] = J[i-1];
		}
		for(int i=1 ; i<=M ; i++)scanf("%d",&P[i]);
		sort(J+1,J+1+N);
		int temp = 1;
		for(int i=1 ; i<=M ; i++){
			temp = lower_bound(J+temp,J+1+N,P[i])-J;
			if(temp == N+1)break;
			else ++ans;
			++temp;
		}
		printf("%d
",ans);
	}
	
	return 0;
} 





原文地址:https://www.cnblogs.com/vocaloid01/p/9514098.html