Codeforces Round #305 (Div. 1) B. Mike and Feet (monotonous stack ),

Source: Internet
Author: User

Codeforces Round #305 (Div. 1) B. Mike and Feet (monotonous stack ),

B. Mike and Feettime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Mike is the president of country What-The-Fatherland. There areNBears living in this country besides Mike. All of them are standing in a line and they are numbered from 1NFrom left to right.I-Th bear is exactlyAIFeet 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 eachXSuch that 1 limit ≤ limitXLimit ≤ limitNThe maximum strength among all groups of sizeX.

Input

The first line of input contains integerN(1 digit ≤ DigitNLimit ≤ limit 2 × hour 105), the number of bears.

The second line containsNIntegers separated by space,A1, bytes,A2, middle..., middle ,...,AN(1 digit ≤ DigitAIMemory ≤ memory 109), heights of bears.

Output

PrintNIntegers in one line. For eachXFrom 1N, Print the maximum strength among all groups of sizeX.

Sample test (s) input
101 2 3 4 5 4 3 2 1 6
Output
6 4 4 3 3 2 2 1 1 1 



Official question, learning monotonous Stack

For eachI, Find the largestJThatAJLatency <latencyAIAnd show itLI(If there is no suchJ, ThenLILimit = Limit 0 ).

Also, find the smallestJThatAJLatency <latencyAIAnd show itRI(If there is no suchJ, ThenRISignature = SignatureNRows + rows 1 ).

This can be done inO(N) With a stack. Pseudo do code of the first part (second part is also like that ):

stack s // initially emptyfor i = 1 to n     while s is not empty and a[s.top()] >= a[i]          do s.pop()     if s is empty          then l[i] = 0     otherwise          l[i] = s.top()     s.push(i)

Consider that you are asked to printNIntegers,Ans1, bytes,Ans2, middle..., middle ,...,AnsN. Obviusly,Ans1 bytes ≥ bytesAns2 records ≥ small... records ≥ smallAnsN.

For eachI, We know thatAICan be minimum element in groups of size 1, limit 2, limit..., limit ,...,RIAccept-Encoding-LIAccept-encoding 1.

Se we need a data structure for us to do this:

We have arrayAns1, bytes,Ans2, middle..., middle ,...,AnsNAnd all its elements are initially equal to 0. Also,NQueries. Each query givesX, Bytes,ValAnd want us to performAns1 bytes = bytesMax(Ans1, bytes,Val), Then ),Ans2 bytes = bytesMax(Ans2, bytes,Val), Please..., please ),...,AnsXSignature = SignatureMax(AnsX, Bytes,Val). We want the final array.

This can be done inO(N) With a maximum partial sum (keeping maximum instead of sum), read here for more information about partial sum.

Time complexity :.

C ++ Code by PrinceOfPersia

C ++ Code by Haghani

Java Code by Zlobober



#include <cstdio>#include <iostream>#include <vector>#include <queue>#include <stack>using namespace std;typedef long long ll;#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)const int inf = 0x3f3f3f3f;const int maxn = 2e5 + 10;int a[maxn],l[maxn],r[maxn],res[maxn];int main(int argc, char const *argv[]){int n;while(scanf("%d",&n)==1) {for(int i = 1; i <= n; i++) scanf("%d",a+i);stack<int>s;for(int i = 1; i <= n; i++) {res[i] = 0;while(!s.empty()&&a[s.top()] >= a[i]) s.pop();if(s.empty()) l[i] = 0;else l[i] = s.top();s.push(i);}while(!s.empty())s.pop();for(int i = n; i >= 1; i--) {while(!s.empty()&&a[s.top()] >= a[i]) s.pop();if(s.empty()) r[i] = n + 1;else r[i] = s.top();s.push(i);}for(int i = 1; i <= n; i++) {int x = r[i] - l[i] -1;res[x] = max(res[x],a[i]);}for(int i = n-1; i > 0; i--) res[i] = max(res[i+1],res[i]);for(int i = 1; i <= n; i++)printf("%d%c", res[i]," \n"[i==n]);}return 0;}




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: info-contact@alibabacloud.com 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.