51nod-1279 扔盘子(单调栈)
来源:互联网 发布:数据库事务是什么意思 编辑:程序博客网 时间:2024/06/06 06:54
1279 扔盘子
收藏
关注
有一口井,井的高度为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 5564362323524
Output示例
4
题解:
直接模拟的话,是O(n^2)的复杂度,分析题中的数据和情景,可以发现对于井的数据从井口到井底必然是是一个有序的不增序列,由此,使用单调栈就可以简单的解决,对井数组well预处理入栈,然后对于没一个盘子在进行出栈操作,每当容纳下一个盘子就计数加一,最后就是结果,详见代码。
代码:
#include<iostream>#include<cstring>#include<math.h>#include<stdlib.h>#include<cstring>#include<cstdio>#include<utility>#include<algorithm>#include<map>#include<stack>using namespace std;typedef long long ll;const int Max = 1e5+5;const int mod = 1e9+7;const int Hash = 10000;const int INF = 1<<30;const ll llINF = 1e18;int n, m;int well[Max], plate[Max];int main( ){ //freopen("input.txt", "r", stdin); while(cin>>n>>m) { int ans = 0, Min = INF; for(int i=0; i<n; i++) scanf("%d", well+i); for(int i=0; i<m; i++) scanf("%d", plate+i); //预处理,用单调栈来处理数据 stack<int> st; for(int i=0; i<n; i++) { if(well[i]<Min) Min = well[i]; st.push(Min); } int k = 0; while(!st.empty() && k<m)//井满或者盘子用尽都结束 { if(st.top()<plate[k])//盘子大继续出栈 { st.pop( ); continue; } ans++;//在这个位置卡住一个盘子 st.pop( ); k++; } cout<<ans<<endl; } return 0;}
阅读全文
0 0
- 51 NOD 1279 扔盘子 单调栈
- 51nod-1279 扔盘子(单调栈)
- 51nod 1279 扔盘子(单调栈)
- 51 NOD 1279 扔盘子(单调栈)
- 51nod 1279 扔盘子(单调栈)
- 【51nod 1279 扔盘子】(单调栈)
- 51Nod 1279 扔盘子 栈模拟
- 1279 扔盘子(51nod)
- 51nod 1279 扔盘子
- 51nod 1279 扔盘子
- 51nod 1279 扔盘子
- 51nod-【1279 扔盘子】
- 51nod-1279 扔盘子
- 51nod 1279 扔盘子
- 51Nod 1279 扔盘子
- 51nod 1279 扔盘子
- 51nod 1279 扔盘子
- 51Nod 扔盘子
- LeetCode 507 Perfect Number(完美数字)
- 神奇的线段树
- Python之Pandas库
- c3p0的配置方式
- Python之NumPy库
- 51nod-1279 扔盘子(单调栈)
- Rsync同步算法
- JSTL标准标签库之格式化标签(三)
- LuoguP2254 瑰丽华尔兹
- Fibonacci
- MySQL联合查询分页
- Hibernate入门04_双向多对一
- 自定义类加载器
- Fiddler(一)模拟弱网环境