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.

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
  1. 10
  2. 1 2 3 4 5 4 3 2 1 6
output
  1. 6 4 4 3 3 2 2 1 1 1
  1.  
  1. 题意:
  1. 给出n个数,这n个数在区间长度为i1~n)的时候能够切割成一些区间。这每一个区间都会有一个最小值。在相同长度的这些区间的最小值中,输出最大值
  1.  
  1. 思路:
  1. 使用单调栈,保持栈内的数的单调递增性
  1.  
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stack>
  5. #include <queue>
  6. #include <map>
  7. #include <set>
  8. #include <vector>
  9. #include <math.h>
  10. #include <bitset>
  11. #include <algorithm>
  12. #include <climits>
  13. using namespace std;
  14. #define LS 2*i
  15. #define RS 2*i+1
  16. #define UP(i,x,y) for(i=x;i<=y;i++)
  17. #define DOWN(i,x,y) for(i=x;i>=y;i--)
  18. #define MEM(a,x) memset(a,x,sizeof(a))
  19. #define W(a) while(a)
  20. #define gcd(a,b) __gcd(a,b)
  21. #define LL long long
  22. #define N 200005
  23. #define MOD 1000000007
  24. #define INF 0x3f3f3f3f
  25. #define EXP 1e-8
  26. #define lowbit(x) (x&-x)
  27. /*
  28. num栈顶的元素,width宽度,跟如今入栈的数之间相隔了多少个数,由于要求的是连续区间。所以这个也是必须记录的
  29. */
  30. struct node
  31. {
  32.     int num,width;
  33.     node() {};
  34.     node(int _num,int _width):num(_num),width(_width) {}
  35. };
  36. stack<node> S;
  37. int a[N],ans[N];
  38. int main()
  39. {
  40.     int n,i,j;
  41.     scanf("%d",&n);
  42.     for(i = 0; i<n; i++)
  43.         scanf("%d",&a[i]);
  44.     a[n++] = 0;
  45.     MEM(ans,0);
  46.     for(i = 0; i<n; i++)
  47.     {
  48.         int len = 0;//连续区间的长度
  49.         node k;
  50.         while(!S.empty())
  51.         {
  52.             k = S.top();
  53.             if(k.num<a[i])
  54.                 break;
  55.                 //新入栈的元素比栈顶元素要小,那么对于这个连续区间而言,这个比新入栈的元素就没实用了,能够出栈
  56.             int ls=k.width+len;//出栈的同一时候获得其长度
  57.             if(k.num>ans[ls])//ans记录ls区间的时候的最大值
  58.             {
  59.                 ans[ls]=k.num;
  60.             }
  61.             len+=k.width;
  62.             S.pop();
  63.         }
  64.         S.push(node(a[i],len+1));
  65.     }
  66.     for(i = n-1; i>=1; i--)//由于上面仅仅更新了一部分的点,所以如今要对那些没有更新的点也更新
  67.         ans[i]=max(ans[i],ans[i+1]);
  68.     printf("%d",ans[1]);
  69.     for(i = 2; i<n; i++)
  70.         printf(" %d",ans[i]);
  71.     printf("\n");
  72.     return 0;
  73. }
  74.  

版权声明:本文博主原创文章,博客,未经同意不得转载。

Codeforces548D:Mike and Feet(单调栈)的更多相关文章

  1. Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈

    B. Mike and Feet Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...

  2. codeforces 547B. Mike and Feet 单调栈

    题目链接 用单调栈计算出一个数字, 左边第一个比他小的数字的位置, 右边比第一个他小的数字的位置, 然后len = r[i] - l[i] +1. ans[len] = max(ans[len], a ...

  3. Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. Codeforces 547B. Mike and Feet[单调栈/队列]

    这道题用单调递增的单调栈维护每个数能够覆盖的最大区间即可. 对于   1 2 3 4 5 4 3 2 1 6 这组样例, 1能够覆盖的最大区间是10,2能够覆盖的最大区间是7,以此类推,我们可以使用单 ...

  5. CF Mike and Feet (求连续区间内长度为i的最小值)单调栈

    Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Mike and Feet CodeForces - 548D (单调栈)

    Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...

  7. Codeforces Round #305 (Div. 2)D. Mike and Feet(单调栈)

    题意 n个值代表n个熊的高度 对于size为x的group strength值为这个group(连续的几个熊)中熊的最小的height值 对于x(1<=x<=n) 求出最大的strengt ...

  8. CodeForces 548D 单调栈

    Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  9. Mike and Feet(CF 547B)

    Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. MFC调试小技巧

    今天看acl源码的时候看到一个函数AllocConsole().百度一下感觉这个函数对于调试非常不错,当然对于MFC里面的调试信息,我都是用TRACE打印自己感兴趣的消息的,而且仅仅有在DEBUG里面 ...

  2. 【iOS】文件上传小记

    iOS由该系统提供API可以实现可以实现文件的上传和下载,有两种方法来. NSURLConnection与NSURLSession. 当中NSURLConnection是使用非常久的的一种方式.NSU ...

  3. ZOJ Monthly, October 2010 ABEFI

    ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...

  4. 编程算法 - 分割数 代码(C)

    分割数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n个无差别的物品, 将它们划分成不超过m组, 求出划分方法数模M的余数. 比如: n= ...

  5. [LeetCode179]Largest Number

    题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...

  6. RH033读书笔记(9)-Lab 10 Understanding the Configuration Tools

    Lab 10 Understanding the Configuration Tools Sequence 1: Configuring the Network with system-config- ...

  7. UVA305 - Joseph(数论 + 打表)

    UVA305 - Joseph(数论 + 打表) 题目链接 题目大意:约瑟夫环问题:n个人围成一圈,每次都淘汰第m个人,问最后一个幸存下来的人的编号. 这题的意思有点不一样,它规定前面的k个人是好人, ...

  8. (2) 用DPM(Deformable Part Model,voc-release4.01)算法在INRIA数据集上训练自己的人体检測模型

    步骤一,首先要使voc-release4.01目标检測部分的代码在windows系统下跑起来: 參考在window下执行DPM(deformable part models) -(检測demo部分) ...

  9. Android studio 中国的垃圾问题解决

    为了获得良好的刚安装Android studio, 实例importproject时刻,你会发现很多中国的文件夹显示异常.例如下面的附图: 为什么会出现这个问题呢,事实上原因非常easy,由于Andr ...

  10. Eclipse+Maven创webapp工程

    1.开启eclipse,右键new-->other,例如以下图找到maven project. 选择maven project,点击next 2.选择maven project后.显示创建mav ...