FZU 2163 多米诺骨牌(单调栈)
来源:互联网 发布:久久网络传奇一条龙 编辑:程序博客网 时间:2024/05/16 15:05
题目链接:FZU 2163 多米诺骨牌
单调栈的题目写起来都差不多,想到了就很简单,而且有个共同特点是用VC交会TLE,G++还挺快的,可能是VC对STL的优化不够好?
一般来讲入栈时比较的是什么单调的就是什么,但是这个题感觉单调的东西有点说不清楚,只可意会不可言传==,还是对单调栈的理解不够吧。不过这个不要紧,做这类题目不需要考虑单调的是什么,只要清楚栈内元素什么时候出栈还有其他元素什么时候入栈就行了。
#include <iostream>#include <cstdio>#include <stack>#include <algorithm>using namespace std;const int MAX_N = 100000 + 100;struct Node{ int i, r, l;};struct xAh{ int x, h, i;};bool cmp(xAh a, xAh b){ return a.x < b.x;}xAh xh[MAX_N];int ans[MAX_N];int main(){ //freopen("in.txt", "r", stdin); int n; while(scanf("%d", &n) != EOF) { stack <Node> S; Node temp, tmp; for(int i = 0; i < n; i++) { scanf("%d%d", &xh[i].x, &xh[i].h); xh[i].i = i; } sort(xh, xh + n, cmp); for(int i = 0; i < n; i++) { temp.i = xh[i].i, temp.l = i; temp.r = xh[i].x + xh[i].h - 1; while(!S.empty() && S.top().r < xh[i].x) { tmp = S.top(); S.pop(); ans[tmp.i] = i - tmp.l; } /*if(!S.empty() && (S.top().r - xh[i].x) > temp.r) temp.r = S.top().r - xh[i].x;*/ S.push(temp); } while(!S.empty()) { tmp = S.top(); S.pop(); ans[tmp.i] = n - tmp.l; } for(int i = 0; i < n - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[n - 1]); } return 0;}
0 0
- FZU 2163 多米诺骨牌(单调栈)
- FZU 2163 多米诺骨牌
- 多米诺骨牌 单调栈
- Problem 2163 多米诺骨牌(单调栈)
- FZU 2163 多米诺骨牌(线段树)
- FZU 1845 Look Up(单调栈)
- 【单调栈】fzu 2136取糖果
- 单调队列 FZU 1894
- FZU 2190 非提的救赎(单调栈)
- Fzu-2190 非提的救赎 (单调栈)
- 【Good Bye 2014E】【贪心 单调栈+线段树】New Year Domino 至少增加多高长度的多米诺骨牌才可推x倒y
- 多米诺骨牌
- 多米诺骨牌
- 多米诺骨牌
- 多米诺骨牌
- 多米诺骨牌
- 多米诺骨牌
- 多米诺骨牌
- 中国互联网企业都是在给外国人打工
- 每天,每周,每月,每年程序员该做的事情
- 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Tes
- 第十一章 字符串和字符串函数(字符串标准输入输出库函数)总结 140525
- 算法java实现--分支限界法--旅行售货员问题
- FZU 2163 多米诺骨牌(单调栈)
- redhat 6.5 firefox打开本地文件乱码问题
- 新调查研究表明Android和iOS设备正慢慢侵蚀PC市场
- 行为型模式5:命令模式(Command Pattern)
- weblogic中使用jms发送和接受消息
- SSH+AJAX遇到的:对action进行注入后不能转为json的问题。
- TimesTen内存库sys.odbc.ini参数说明
- 各种网易黄金激活码,新手码,实时更新可用,点击图片领取,真正的福利,快人一步
- 手机找回