Codeforces548D Mike and Feet 数据结构+单调栈+优先队列
来源:互联网 发布:决战武林法器进阶数据 编辑:程序博客网 时间:2024/06/05 15:08
Description
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 from1 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 strength of a group is the minimum height of the bear in that group.
Mike is a curious to know for each x such that1 ≤ x ≤ n the maximum strength among all groups of sizex.
Input
The 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.
Output
Print n integers in one line. For each x from 1 to n, print the maximum strength among all groups of size x.
Sample Input
101 2 3 4 5 4 3 2 1 6
6 4 4 3 3 2 2 1 1 1
解题思路:
仔细的想了好久,发现这题就是一个单调栈问题,正好前段时间正好做过。
1,对于每个数要找他的影响范围,那么就是这个数左边第一个比他小的数,和右边第一个比他小的数字之间。
2,那么问题来了怎么找每一个数的这个区间呢,单调栈可以解决这个问题,参考我的博客
http://blog.csdn.net/fans_ac/article/details/51965804
还算详细的介绍了单调栈的问题,那么这个区间就很好的获取了。
3,获取区间了,怎么求得对于每一个1~k的区间的最大值呢。优先队列可以很好的解决这个问题。
4,优先队列每次都抛出一个最大值,这个最大值的区间就是这个区间的最大值,后面来的都没有大。
5,弹完队列里面所有的数据,就可以找到所有的结果了。
#include<cstdio>#include<cstring>#include<iostream>#include<stack>#include<queue>using namespace std;const int maxn = 200005 ;struct node{ int val; int pos ;};struct node2{ int len ; int val ; bool operator < (const node2 & p)const{ return val<p.val ; }} ;stack<node> qq ;int arry[maxn] ;int L[maxn] ;int R[maxn] ;priority_queue<node2> pp ;int main(){ int n ; while(~scanf("%d",&n)){ for(int i=1;i<=n;i++){ scanf("%d",&arry[i]); } while(!qq.empty())qq.pop(); node def; def.pos = 0 ; def.val = -0x3f3f3f3f ; //printf("eys"); qq.push(def); for(int i = 1;i<=n;i++){ //printf("i = %d\n",i); while(qq.top().val>=arry[i]){ qq.pop(); } //printf("s\n"); node t ; t.val = arry[i] ; t.pos = i ; L[i] = qq.top().pos+1; qq.push(t); } //printf("eys"); while(!qq.empty())qq.pop(); def.pos = n+1 ; def.val = -0x3f3f3f3f ; qq.push(def); for(int i = n;i>=1;i--){ while(qq.top().val>=arry[i]){ qq.pop(); } node t ; t.val = arry[i] ; t.pos = i ; R[i] = qq.top().pos-1; qq.push(t); }// for(int i =1;i<=n;i++){// printf("%d %d\n",L[i],R[i]);// } while(!pp.empty())pp.pop(); for(int i = 1;i<=n;i++){ node2 newnode ; newnode.len = R[i] - L[i] + 1 ; newnode.val = arry[i] ; pp.push(newnode) ; } int ans[maxn]; memset(ans,0,sizeof(ans)); while(!pp.empty()){ for(int i=pp.top().len;i>=1;i--){ if(ans[i]!=0)break ; else ans[i]=pp.top().val ; } pp.pop(); } printf("%d",ans[1]); for(int i = 2;i<=n;i++){ printf(" %d",ans[i]); }printf("\n"); } return 0;}
- Codeforces548D Mike and Feet 数据结构+单调栈+优先队列
- Codeforces548D:Mike and Feet(单调栈)
- CodeForces548D-Mike and Feet(单调栈)
- Mike and Feet(单调栈)
- Codeforces #548D Mike and Feet(单调栈)
- CodeForces - 548D Mike and Feet(单调栈)
- Codeforces 547 B - Mike and Feet (单调栈)
- Codeforces Round #305 Mike and Feet(单调栈)
- codeforces #305 547B B. Mike and Feet(单调栈)
- Codeforces 547B Mike and Feet(单调栈)
- Codeforces 547B - Mike and Feet (单调栈)
- CodeForces 548D Mike and Feet (单调栈)
- codeforces 548 D. Mike and Feet (单调栈)
- CodeForces 547B - Mike and Feet(单调栈)
- CF D. Mike and Feet 单调栈的运用
- 【数据结构】 CF 547B Mike and Feet
- Codeforces Round #305 (Div. 2)D---Mike and Feet(单调栈)
- Codeforces Round #305 (Div. 1) B. Mike and Feet(单调栈)
- 将openfire源码部署到IDEA中
- spring ioc原理
- 当我在荒废时间的时候会有多少人在拼命
- 大话安卓应用开发-目录
- JS中的原型类
- Codeforces548D Mike and Feet 数据结构+单调栈+优先队列
- jQuery对象和JavaScript对象之间的转换!jQuery党情何以堪啊!
- android中一个TextView实现其中内容大小颜色不同的效果
- 移动端屏幕适配
- Intellij Idea识别Java Web项目
- 10个值得深思的PHP面试问题
- linux中生成shh公用密匙的方法,和快捷配置。
- Drupal -- 介绍001
- Windows 平台下面向开发者的ftp服务器