Maximum output sequence and T4312 stack sequence of T4312

Source: Internet
Author: User

Maximum output sequence and T4312 stack sequence of T4312
Description

This gives you a stack and n numbers, which are imported into the stack in the order of n numbers. You can choose to set the number at any time.

To maximize the Lexicographic Order of the stack sequence.

Input/Output Format Input Format:

Enter two rows in total.

The first line is an integer n, indicating the length of the inbound stack sequence.

The second row contains n integers, indicating the inbound stack sequence.

Output Format:

Only one row, n integers in total, indicates the output stack sequence you calculated.

Input and Output sample Input example #1:
32 1 3
Output sample #1:
3 1 2
Description

For 100% of data, 1 ≤ n ≤ 10 6, all the numbers read are not repeated, that is, they must be arranged.

 

 1 #include <cstdio> 2 #include <stack> 3 using namespace std; 4 int const MAX = 1e6 + 2; 5 int cur[MAX], pos[MAX], num[MAX]; 6 stack <int> s; 7 int main() 8 { 9     int n;10     scanf("%d",&n);11     for(int i=0;i<n;i++)12         scanf("%d", &num[i]);13     for(int i=n;i>0;i--) 14     {15         if(cur[i] > num[i - 1])16         {17             cur[i - 1] = cur[i];18             pos[i - 1] = pos[i];19         }20         else21         {22             cur[i - 1] = num[i - 1];23             pos[i - 1] = i - 1;24         }25     }26     for(int j = 0, i = 0; i < n; i++)27     {28         if(s.empty() || s.top() < cur[j])29         {30             for(int k = pos[j]; j <= k; j++)31                 s.push(num[j]);32         }33         if(i != n - 1)34         {35             printf("%d ", s.top());36             s.pop();37         }38         else39             printf("%d\n", s.top());40     }41 }

 

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.