July month race T2

Title Description

"X Dragon Ball" is a puzzle game. The game has  n- ( 2 | n- ) numbered in accordance with mutually different Pearl arranged in a given order in the queue, it has a number above each Pearl. Each operation, selecting and outputting the two adjacent Pearl Pearl queue, the queue into the end of the destination (the destination queue is initially empty, and the same order before and after the two Pearl), and then removing the original queue Pearl void. He repeated several times until the original Dragon Ball queue is empty. Visible because the decision does not lead to the same destination queue order is not the same. Now all the requests, the target maximum queue lexicographically scheme. Only you need to give the destination queue can be.

For example, when the original Pearl queue is [1,3,2,4], it is possible to remove the 2 and 3, the destination queue at this time is [3,2], the original queue is Pearl [1,4]; then left Pearl into two destination queue, the destination queue is obtained [3,2,1,4]

Input and output formats

Input formats:

 

The first line, an integer  n- .

The next line, each line  n  integers, indicates the number of the original Pearl queue.

 

Output formats:

 

Row, n-  integers.

 

Sample input and output

Input Sample # 1:  Copy
4 
3 1 4 2
Output Sample # 1:  Copy
4 2 3 1
Input Sample # 2:  Copy
6 
6 5 4 1 3 2
Output Sample # 2:  Copy
6 5 4 1 3 2

Explanation

For 20% of the data, n- . 1 0.
For 60% of the data, n- . 1 0 . 3 .
To 100% of the data, n- . 1 0 . 5 , Pearl number not exceeding n.

Ideas:

Pearl this thing is a good thing, heard students say that good looking. . .

This problem chiefs Yipaitaitui room, unhesitatingly say something on this tall tree, konjac would not ah (in fact, too lazy to fight ...) , a closer look at the title, this title is not yet linked list , but the pointer does not seem to ah. . .

Then use an array of simulated about it. . .

Able to save a list with every point of a front and rear, and then look for the big to the small, only several later when he will not used in conjunction with a point behind him with the output, the number in front of him and after output behind him on the back and then gone even number

Code

 1 #include<iostream>
 2 using namespace std;
 3 const int maxn=1e6+10;
 4 int sum[maxn],head[maxn],next[maxn];
 5 int main()
 6 {
 7     int n,m;
 8     cin>>n;
 9     for(int i=1;i<=n;i++)
10     {
11         cin>>sum[i];
12         next[sum[i-1]]=sum[i];
13         head[sum[i]]=sum[i-1];
14     }
15     for(int i=n;i>=1;i--)
16     {
17         if(next[i])
18         {
19             cout<<i<<" "<<next[i]<<" ";
20             next[head[i]]=next[next[i]];
21             head[next[head[i]]]=head[i];
22             next[next[i]]=0;    
23         }
24     }
25     cout<<endl;
26     return 0;
27 }

 

Guess you like

Origin www.cnblogs.com/2529102757ab/p/11205606.html