Codeforces548d:mike and Feet (monotonic stack)

Source: Internet
Author: User

Mike is the president of country What-the-fatherland. There isNBears living in this country besides Mike. All of them is standing in a line and they is numbered from1ToNFrom 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 "the Bear" that group.

Mike is a curious to know for each x such this 1?≤? x? ≤? n The maximum strength among all groups of size X.


The first line of input contains integer n (1?≤? N? ≤?2?x?105), the number of bears.

The second line containsNIntegers separated by space, a1,? a 2,?...,? a N (1?≤? a i? ≤?109 ), heights of bears.


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
101 2 3 4 5 4 3 2 1 6
Test instructions
Given the number of n, the n number can be divided into intervals when the interval length is I (1~n), each of which will have a minimum value, and the maximum value of these intervals in the same length
Use a monotonic stack to keep the number in the stack monotonically increasing
#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 stack the element at the top of the width, and the number of times separated from the current into the stack, because a continuous interval is required, so this also must be recorded */struct node{int num,width    ;    Node () {};    Node (int _num,int _width): num (_num), Width (_width) {}};stack<node> s;int a[n],ans[n];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;Length of continuous interval node k; while (!            S.empty ()) {k = ();                if (K.num<a[i]) break; The new stack element is smaller than the top of the stack, so for this continuous interval, this is more than the new stack of elements is useless, you can go out of the stack int ls=k.width+len;//out the stack and get its length if (K.num>ans[ls]            The maximum value of//ans recording ls interval {ans[ls]=k.num;            } len+=k.width;        S.pop ();    } S.push (Node (a[i],len+1));    } for (i = n-1; i>=1; i--)//Because the above only updates a subset of the points, the Ans[i]=max (Ans[i],ans[i+1]) is now updated for those points that are not updated;    printf ("%d", ans[1]);    for (i = 2; i<n; i++) printf ("%d", ans[i]);    printf ("\ n"); return 0;}

Codeforces548d:mike and Feet (monotonic stack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.