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 }