1. 程式人生 > >單調棧 左右傳訊息 HDU 3410

單調棧 左右傳訊息 HDU 3410

                                                  Passing the Message

What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten are prepared to have an excursion. Before kicking off, teacher Liu tells them to stand in a row. Teacher Liu has an important message to announce, but she doesn’t want to tell them directly. She just wants the message to spread among the kids by one telling another. As you know, kids may not retell the message exactly the same as what they was told, so teacher Liu wants to see how many versions of message will come out at last. With the result, she can evaluate the communication skills of those kids. 
Because all kids have different height, Teacher Liu set some message passing rules as below: 

1.She tells the message to the tallest kid. 

2.Every kid who gets the message must retell the message to his “left messenger” and “right messenger”. 

3.A kid’s “left messenger” is the kid’s tallest “left follower”. 

4.A kid’s “left follower” is another kid who is on his left, shorter than him, and can be seen by him. Of course, a kid may have more than one “left follower”. 

5.When a kid looks left, he can only see as far as the nearest kid who is taller than him. 

The definition of “right messenger” is similar to the definition of “left messenger” except all words “left” should be replaced by words “right”. 

For example, suppose the height of all kids in the row is 4, 1, 6, 3, 5, 2 (in left to right order). In this situation , teacher Liu tells the message to the 3rd kid, then the 3rd kid passes the message to the 1st kid who is his “left messenger” and the 5th kid who is his “right messenger”, and then the 1st kid tells the 2nd kid as well as the 5th kid tells the 4th kid and the 6th kid. 
Your task is just to figure out the message passing route.

Input

The first line contains an integer T indicating the number of test cases, and then T test cases follows. 
Each test case consists of two lines. The first line is an integer N (0< N <= 50000) which represents the number of kids. The second line lists the height of all kids, in left to right order. It is guaranteed that every kid’s height is unique and less than 2^31 – 1 .

Output

For each test case, print “Case t:” at first ( t is the case No. starting from 1 ). Then print N lines. The ith line contains two integers which indicate the position of the ith (i starts form 1 ) kid’s “left messenger” and “right messenger”. If a kid has no “left messenger” or “right messenger”, print ‘0’ instead. (The position of the leftmost kid is 1, and the position of the rightmost kid is N)

Sample Input

2
5
5 2 4 3 1
5
2 1 4 3 5

Sample Output

Case 1:
0 3
0 0
2 4
0 5
0 0
Case 2:
0 2
0 0
1 4
0 0
3 0

題意:對每個a[i]求出左右兩邊,比自己小的且最大的元素

思路:左邊,建立從左至右的單調遞減棧:

                          a[i]>top,出棧並每次記錄top,直至a[i]>top,此時上一個top就是比a[i]小的左邊最大元素;

                          a[i]<top,由於棧單調遞減,棧頂元素是最小的所以,此時無符合的元素。

                          空棧,不存在符合的元素。

       右邊同理。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn=50005;

int a[maxn];
int l[maxn];
int r[maxn];
int main()
{
    int t;
    int n;
    scanf("%d",&t);
    int ans=1;
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        stack<int> st;
        memset(l,0,sizeof l);
        memset(r,0,sizeof r);
        for(int i=1;i<=n;i++)//找到左邊最高的,從左往右單調遞減棧
        {
            if(st.empty())
            {
                l[i]=0;
                st.push(i);
            }
            else if(a[i]>a[st.top()])
            {
                while(!st.empty()&&a[i]>a[st.top()])
                {
                    l[i]=st.top(); //top將會被彈出棧,top滿足比a[i]小且在左邊最大
                    st.pop();
                }
                st.push(i);
            }
            else if(a[i]<a[st.top()])
            {
                l[i]=0;//左邊沒有比他小的
                st.push(i);
            }
        }
        stack<int> p;
        for(int i=n;i>=1;--i)//找右邊的比自己小的最大值,從右往左建立遞減棧
        {
            if(p.empty()) r[i]=0;
            else if(a[i]>a[p.top()])
            {
                while(!p.empty()&&a[i]>a[p.top()])
                {
                    r[i]=p.top();
                    p.pop();
                }
            }
            else if(a[i]<a[p.top()])
            {
                r[i]=0;
            }
            p.push(i);
        }
        cout << "Case " <<  ans++<<  ":" << endl;
		for(int i = 1;i <= n;i++)
		cout << l[i] << " " << r[i] << endl;

    }
}