Codeforces 547B Mike and Feet
2015-11-06 17:42
239 查看
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 strength of 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.
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 test(s)
input
10
1 2 3 4 5 4 3 2 1 6
output
6 4 4 3 3 2 2 1 1 1
解题思路:单调队列的典型应用,首先对于每一个元素a[i],我们求解出a[i]最左边和最右边能扩展到的位置,使得在这个区间内a[i]的值是最小的。其中l[i]和r[i]分别从头到尾和从尾到头利用单调队列扫两遍即可求出。这样对于没一个a[i]它能掌控的最大区间范围我们便可以求解出,这样我们便可以求解出部分ans[i],又因为ans[i]随着i的增大是单调不增的,我们需要n->1求解没有更新的ans[i],即ans[i]=max(ans[i], ans[i+1]).
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 that 1 ≤ x ≤ n the maximum strength among all groups of size x.
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 test(s)
input
10
1 2 3 4 5 4 3 2 1 6
output
6 4 4 3 3 2 2 1 1 1
解题思路:单调队列的典型应用,首先对于每一个元素a[i],我们求解出a[i]最左边和最右边能扩展到的位置,使得在这个区间内a[i]的值是最小的。其中l[i]和r[i]分别从头到尾和从尾到头利用单调队列扫两遍即可求出。这样对于没一个a[i]它能掌控的最大区间范围我们便可以求解出,这样我们便可以求解出部分ans[i],又因为ans[i]随着i的增大是单调不增的,我们需要n->1求解没有更新的ans[i],即ans[i]=max(ans[i], ans[i+1]).
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <vector> #include <deque> #include <queue> #include <map> #include <set> #include <utility> #include <algorithm> #include <functional> using namespace std; const int maxn = 2*100010; const int inf = 0x3f3f3f3f; deque< pair<int, int> > dq; int a[maxn], l[maxn], r[maxn]; int ans[maxn]; int n; int main() { //freopen("aa.in", "r", stdin); scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } dq.push_back(make_pair(0, 0)); for(int i = 1; i <= n; ++i) { while(dq.back().first >= a[i]) { dq.pop_back(); } l[i] = dq.back().second + 1; dq.push_back(make_pair(a[i], i)); } while(!dq.empty()) dq.pop_back(); dq.push_back(make_pair(0, n + 1)); for(int i = n; i >= 1; --i) { while(dq.back().first >= a[i]) dq.pop_back(); r[i] = dq.back().second - 1; dq.push_back(make_pair(a[i], i)); } memset(ans, 0, sizeof(ans)); for(int i = 1; i <= n; ++i) { ans[r[i]-l[i]+1] = max(a[i], ans[r[i]-l[i]+1]); } for(int i = n - 1; i >= 1; --i) { ans[i] = max(ans[i], ans[i+1]); } printf("%d", ans[1]); for(int i = 2; i <= n; ++i) { printf(" %d", ans[i]); } printf("\n"); return 0; }
相关文章推荐
- JSP基本语法--Page指令 <%@page 属性=”内容“%>
- jquery ajax form serialize() 转json 数据,post 后台
- jQuery选择器大全
- 关于jquery--index()方法的简单理解
- JS 有关时间处理
- AngularJS开发指南03:HTML编译器
- JQuery开发工具和插件
- AngularJS开发指南02:引导程序
- 前端基础3
- 【JQuery】sortable.js Uncaught TypeError $(...).sortable is not a function
- 求解答:JS控制按钮切换页面,实现显示公共的部分前台代码如下:
- AngularJS开发指南01:AngularJS简介
- selenium webdriver 执行javascript代码
- 原生javascript制作无缝滚动
- JS中获取select、input的参数
- 前端工程师如何打发闲余时光?(转)
- Jquery的on,off事件详解,如何利用命名空间来解绑特定的事件
- Javascript闭包
- jQuery attr removeAttr 属性操作
- JS中window()、href()传递参数