单调栈【小记】
来源:互联网 发布:dwf转cad软件 编辑:程序博客网 时间:2024/05/21 08:38
第一次接触单调栈是暑假的时候,那时做到了了 HDU上的一题 : Largest Rectangle in a Histogram
后来在范总的帮助下敲掉了。
今天决定重新坐下这类题,目的是完善下代码风格和熟悉下。
题目都是蛮简单的,核心算法就是用单调栈处理出第i个点左右最长可以延伸到的位置(记做 : L[i] 和 R[i])。
挂了三道POJ题目 :
1 Terrible Sets : (题意有点坑而已,理解后会发现和HDU上那题是一样的)
2 Feel Good : (首先O(n)预处理出任意区间和,然后注意下会超int需要用__int64来保存区间和和最后的答案)
3 Sticks Problem: (单调栈预处理后,我是用O(n^2)寻找最后的答案的,如果从R[i] 的最右端找过来,虽然看似O(n^2),但是如果符合要求直接break的话还是可以过的)
最后贴一个单调栈预处理的代码 :
void init(){ sta[0] = 0; cnt = 0; for (int i = 1;i <= n;i++) { if (a[i] >= sta[cnt]) { sta[++cnt] = a[i]; tot[cnt] = i; } else { while (a[i] < sta[cnt]) { R[tot[cnt]] = i - 1; cnt--; } sta[++cnt] = a[i]; tot[cnt] = i; } } for (int i = 1;i <= cnt;i++)R[tot[i]] = n; sta[0] = 0; cnt = 0; for (int i = n;i >= 1;i--) { if (a[i] >= sta[cnt]) { sta[++cnt] = a[i]; tot[cnt] = i; } else { while (a[i] < sta[cnt]) { L[tot[cnt]] = i + 1; cnt--; } sta[++cnt] = a[i]; tot[cnt] = i; } } for (int i = 1;i <= cnt;i++)L[tot[i]] = 1;}
- 单调栈【小记】
- 单调栈 单调队列
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- 单调栈
- MyEclipse2013 svn 安装方法
- MySql java驱动版本对照关系
- 机器学习中的相似性度量
- 手机上网的秘密
- 深入浅出CChart 每日一课——第十一课 分层视图,楼上七班的女孩
- 单调栈【小记】
- 【安全】Snort 2.9.5.5 网络入侵检测系统
- Java 基础加强 - 类加载器
- 深入JVM锁机制1-synchronized
- 深入JVM锁机制2-Lock
- 数学专项counting:LA 3295
- 第一周poj1845
- Tomcat正常启动后,报404错误
- 启动SSH-server出现错误。Package openssh-server is not available, but is referred to by another package.