Uvalive 6531 Go up the Ultras monotone stack +rmq

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions

Given N-Mount Mountain

The following n numbers indicate the height of Mount N


If this mountain is lawful, you must satisfy:

1, if you have a higher than u on the left side of the mountain, set V is U left distance u recent and strictly higher than U mountain, between [v,u] at least one mountain X, so that the height difference between x and U >=15000

2, the right also the same.

It is lawful to satisfy 1 or 2 at the same time.

Ask:

Output all valid mountains.

Ideas:

To find a point near the nearest mountain is to maintain a monotonous stack, and then to the height of the mountain to seek a rmq.

Write and write ...

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <bits/stdc++.h>template <class t>inline    BOOL Rd (T &ret) {char c; int sgn;    if (C=getchar (), c==eof) return 0; while (c!= '-' && (c< ' 0 ' | |    C> ' 9 ')) C=getchar ();    sgn= (c== '-')? -1:1;    ret= (c== '-')? 0: (c ' 0 ');    while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') ret=ret*10+ (c ' 0 ');    RET*=SGN; return 1;}        Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-');    x =-X;    } if (X>9) pt (X/10); Putchar (x%10+ ' 0 ');}  Using namespace Std;typedef long long ll;const int n = 100050;int d[n*2][20];void rmq_init (int *a, int N) {for (int i = 1; I <= N; ++i) D[i][0] = a[i];for (int j = 1; (1 << j) <= N; ++J) for (int i = 1; i + j-1 <= N; ++i) {D[i][j] = min (d[i][j-1], d[i + (1 << (j-1))][j-1]);}} int RMQ (int L, int R) {int k = 0;while ((1 << (k + 1)) <= R-l + 1) ++k;return min (d[l][k], D[r-(1 << k) + 1][k]);} int n, H[n], StAck[n], Top;int lh[n], rh[n];void work () {for (int i = 1; I <= N; i++) Rd (H[i]);    top = 0;        for (int i = 1, i <= N; i++) {while (top && h[stack[top-1]] <= h[i]) top--;        if (top) lh[i] = stack[top-1];        else lh[i] = 0;    stack[top++] = i;    } top = 0;        for (int i = n; i; i--) {while (top && h[stack[top-1]]<= h[i]) top--;        if (top) rh[i] = stack[top-1];        else rh[i] = 0;    stack[top++] = i;        }}const int hehe = 150000;vector<int>ans;int Main () {while (cin>>n) {work ();        Rmq_init (h, N);        Ans.clear ();            for (int i =1; i <= n; i++) {if (lh[i] = = 0 && rh[i] = = 0) ans.push_back (i);                else if (lh[i] = = 0) {int v = RMQ (i, rh[i]);            if (H[i]-v >= hehe) ans.push_back (i);                } else if (rh[i] = = 0) {int v = RMQ (lh[i], i);               if (H[i]-v >= hehe)      Ans.push_back (i);                } else {int u = RMQ (Lh[i], i), V = RMQ (i, rh[i]);                int maxx = max (U, v);            if (H[i]-maxx >= hehe) ans.push_back (i);            }} for (int i = 0; i < ans.size (); i++) {pt (ans[i]);            if (I==ans.size ()-1) puts ("");        else Putchar ("); }}return 0;}


Uvalive 6531 Go up the Ultras monotone stack +rmq

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.