Small Z love sequence (noip (sang) heart (bin) race) from Fall_dream (algorithm parsing of coarse monotone queue & monotone Stack)

Source: Internet
Author: User

Original question:

Small z is best at solving sequence problems, What is the longest public rise and then down then up the subsequence, small z is easy to solve it.

But little z is not good at sequencing problems, so it gives you a sign-in Question.

Given an n number of sequence ai, you ask for the number of point pairs that meet the following CRITERIA.

Suppose the point pair is (i, j), Max (l,r) is the value of the largest AI in [l,r].

This point pair satisfies the condition when and only if I+1<j and AI < Max (i+1,j-1) < AJ

For simplicity, the input is guaranteed to be an 1-n Arrangement. Believe You're going to do it?

input / output Format

Input data The first line has a number n, and then the second row has n number.

The output contains only one number, representing the number of pairs that meet the CRITERIA.

Sample Input / Output

Input:

5

1 2 3) 4 5

Output:

6

Sample explanation

The conditions were met (1,3), (1,4), (1,5), (2,4), (2,5), (3,5)

Data scope and conventions

for 50% of data, meet n<=300

For 95% of data, meet n<=10000

For 100% of data, meet n<=1000000

All right, I admit it.

I didn't learn all the data structures in this Exam. (konjac Konjac Shivering.) )

First see the topic n<=1000000

So the O (N) algorithm

however, regardless of the line tree or other algorithm maintenance max is O (nlogn) (TLE 95 points)

So...

We need a new data structure!

Seniors say this problem, the water is very.

one on the monotone stack. (never learned ...) )

And then hastened to fill up the Special.

A monotone stack is like a simplified version of a monotone Queue.

But head is 1, and the head of the team never Changes.

obviously, if we search from N to 1, A[i] has a contribution to the topic, and only if a[i] has 2 bigger numbers than a[i] on the right.

and its contribution to the answer is to the right than it-2 (point to condition see topic.) )

So..

It's very Simple. (though I didn't write it in the exam room.) )

The following is a common implementation of monotonic queues and monotonic stacks

first, Let's look at the monotone queue and the operation of the monotonic Stack.

1. Insert an element into the Queue.

2, will be larger than this element or the element is equal to the element kicked out of the queue

1 Code implementation: que[++tail]=a[i];

2 Code implementation: while (head<=tail&&que[tail]>=a[i]) tail--;

of course, Some topics also need to move the team head forward: while (condition) head++;

Then we combine 2 operations, which is the monotone queue,

We only need to output que[head] at the time of the Query. (if It is a monotonic stack, the output que[1]);

Because each element is in the team once, out of the team once

Complexity of averaging o (N) query o (1);

So soon Qaq

Then why do we have to use a monotonous stack?

this, and I understand test instructions. Maintenance Interval min,,

Why did you search from N to 1? (after a long time to Understand)

Because this problem is determined to be the interval max, so if we search from 1 to n, we can only maintain the minimum interval, and the elements in the queue will be squeezed out by the subsequent elements (no statistical answer), because the monotone queue cannot be inserted, can only be deleted with the tail pointer. So we're going from N to 1, qaq.

Orz Zxyer (1 hours ak)

Paste the following code

#include <iostream>#include<cstdio>using namespacestd;intt,n;unsignedLong Longans;inta[1000001];intque[1000001];intmain () {freopen ("seq.in","R", stdin); Freopen ("Seq.out","W", stdout); scanf ("%d",&n);  for(intI=1; I<=n;i++) scanf ("%d",&a[i]);  for(inti=n;i>=1; i--)    {         while((t) && (que[t]<a[i])) {t--;} que[++t]=a[i]; if(t2>0) ans+=t-2ll; } printf ("%lld\n", ans);    Fclose (stdin); Fclose (stdout);} 

Small Z love sequence (noip (sang) heart (bin) race) from Fall_dream (algorithm parsing of coarse monotone queue & monotone 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: 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.