Codeforces548D:Mike and Feet(单调栈)
2015-06-12 17:32
417 查看
Mike is the president of country What-The-Fatherland. There are n bears living in this country besides Mike. All of them are standing in a line and they are numbered from 1 to n from left to right. i-th bear is exactly ai feet high.
A group of bears is a non-empty contiguous segment of the line. The size of a group is the number of bears in that group. The strengthof a group is the minimum height of the bear in that group.Mike is a curious to know for each x such that 1 ≤ x ≤ n the maximum strength among all groups of size x.InputThe first line of input contains integer n (1 ≤ n ≤ 2 × 105), the number of bears.The second line contains n integers separated by space, a1, a2, ..., an (1 ≤ ai ≤ 109), heights of bears.OutputPrint n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.Sample test(s)input
![](http://codeforces.com/predownloaded/6f/7e/6f7e66d850f92c139c12ec4834025f60d1a6a4b7.png)
10 1 2 3 4 5 4 3 2 1 6output
6 4 4 3 3 2 2 1 1 1
题意:
给出n个数,这n个数在区间长度为i(1~n)的时候可以分割成一些区间,这每个区间都会有一个最小值,在同样长度的这些区间的最小值中,输出最大值
思路:
使用单调栈,保持栈内的数的单调递增性
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;#define LS 2*i#define RS 2*i+1#define UP(i,x,y) for(i=x;i<=y;i++)#define DOWN(i,x,y) for(i=x;i>=y;i--)#define MEM(a,x) memset(a,x,sizeof(a))#define W(a) while(a)#define gcd(a,b) __gcd(a,b)#define LL long long#define N 200005#define MOD 1000000007#define INF 0x3f3f3f3f#define EXP 1e-8#define lowbit(x) (x&-x)/*num栈顶的元素,width宽度,跟现在入栈的数之间相隔了多少个数,因为要求的是连续区间,所以这个也是必须记录的*/struct node{ int num,width; node() {}; node(int _num,int _width):num(_num),width(_width) {}};stack<node> S;int a,ans;int main(){ int n,i,j; scanf("%d",&n); for(i = 0; i<n; i++) scanf("%d",&a[i]); a[n++] = 0; MEM(ans,0); for(i = 0; i<n; i++) { int len = 0;//连续区间的长度 node k; while(!S.empty()) { k = S.top(); if(k.num<a[i]) break; //新入栈的元素比栈顶元素要小,那么对于这个连续区间而言,这个比新入栈的元素就没有用了,可以出栈 int ls=k.width+len;//出栈的同时获得其长度 if(k.num>ans[ls])//ans记录ls区间的时候的最大值 { ans[ls]=k.num; } len+=k.width; S.pop(); } S.push(node(a[i],len+1)); } for(i = n-1; i>=1; i--)//因为上面只更新了一部分的点,所以现在要对那些没有更新的点也更新 ans[i]=max(ans[i],ans[i+1]); printf("%d",ans[1]); for(i = 2; i<n; i++) printf(" %d",ans[i]); printf("\n"); return 0;}
相关文章推荐
- jQuery.extend 函数详解
- 使用facebook infer寻找项目潜在BUG
- HTML5通过js调用手机摄像头
- [每天读书半小时] 2015-6-12 Servlet / JSP
- jQuery checkbox 全选
- 201506121154_《JavaScript权威指南(第六版)——垃圾回收机制、列宁认识逻辑方法》(P161-?)
- phoneGap--jQuery Mobile--Html5的一些总结
- 使用Emmet加速Web前端开发
- javascript 编程题目
- String与StringBuffer的区别
- JQuery的Ajax跨域请求的
- jsp页面完美的分页代码,每次显示十条分页链接。
- 文件逐行写入BufferedWriter的刷新flush()方法
- jquery dataTable 自定义 Button及按钮事件
- nodejs之child_process子进程模块
- Emmet:HTML/CSS代码快速编写神器
- js笔记
- 转载-Javascript定义类(class)的三种方法
- Populating Next Right Pointers in Each Node
- JS如何在指定的位置输出一段js脚本