51nod 2级算法题-1279
来源:互联网 发布:京东与淘宝的区别 编辑:程序博客网 时间:2024/06/05 10:39
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 5564362323524
Output示例
4
思路:
转换成一个单调非递增的栈就好了。底下的井宽度大于上面的井的宽度也是无用的!
#include <iostream>yhe#include <cstring>#include <string>#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <queue>#include <stack>using namespace std;#define endl "\n"const int maxn = 5e4+10;int W[maxn];int D[maxn];int main (){ std::ios::sync_with_stdio(false); int n,m; cin>>n>>m; for(int i=0;i<n;i++){ cin>>W[i]; } for(int i=0;i<m;i++){ cin>>D[i]; } int cnt=W[0]; for(int i=0;i<n;i++){ if(cnt>W[i]){ cnt=W[i]; } W[i]=cnt; } int sum=0; int j=n-1; for(int i=0;i<m;i++){ while(D[i]>W[j]){ j--; } if(j<0){ break; } j--; sum++; } cout<<sum<<endl; return 0;}
0 0
- 51nod 2级算法题-1279
- 51nod 2级算法题-1007
- 51nod 2级算法题-1010
- 51nod 2级算法题-1014
- 51nod 2级算法题-1050
- 51nod 2级算法题-1062
- 51nod 2级算法题-1092
- 51nod 2级算法题-1095
- 51nod 2级算法题-1119
- 51nod 2级算法题-1126
- 51nod 2级算法题-1133
- 51nod 2级算法题-1315
- 51nod 2级算法题-1428
- 【模拟】1432 独木舟【51nod】【难度:2级算法题】
- 51nod 1级算法题
- 51nod 3级算法题-1065
- 51nod 5级算法题
- 【贪心+优先队列】1428 活动安排问题【51nod】【难度:2级算法题】
- 外观模式
- PAT甲级1131
- 面试-spring scope作用域
- C语言中动态内存分配
- select/prefetch-related
- 51nod 2级算法题-1279
- 杂谈-01
- 数组遍历删除
- struts2中服务器端数据校验
- GDOI2017模拟 Round 4 4.24-4.26
- C#easyui实现增删改查
- 3ds Max 2014插件安装(插件无效的原因~)
- 决策树
- Android XML解析方式