首页 > 其他 > 详细

HDU6215

时间:2017-09-20 00:16:41      阅读:423      评论:0      收藏:0      [点我收藏+]

Brute Force Sorting

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1204    Accepted Submission(s): 314


Problem Description

Beerus needs to sort an array of N integers. Algorithms are not Beerus‘s strength. Destruction is what he excels. He can destroy all unsorted numbers in the array simultaneously. A number A[i] of the array is sorted if it satisfies the following requirements.
1. A[i] is the first element of the array, or it is no smaller than the left one A[i?1].
2. A[i] is the last element of the array, or it is no bigger than the right one A[i+1].
In [1,4,5,2,3], for instance, the element 5 and the element 2 would be destoryed by Beerus. The array would become [1,4,3]. If the new array were still unsorted, Beerus would do it again.
Help Beerus predict the final array.
 

 

Input

The first line of input contains an integer T (1T10) which is the total number of test cases.
For each test case, the first line provides the size of the inital array which would be positive and no bigger than 100000.
The second line describes the array with N positive integers A[1],A[2],?,A[N] where each integer A[i] satisfies 1A[i]100000.
 

 

Output

For eact test case output two lines.
The first line contains an integer M which is the size of the final array.
The second line contains M integers describing the final array.
If the final array is empty, M should be 0 and the second line should be an empty line.
 

 

Sample Input

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

 

Sample Output

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

 

Source

 
 1 //2017-09-19
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 const int N = 110000;
10 
11 int a[N], n;
12 int _next[N], _pre[N];
13 int head, tail, que[N<<1];
14 
15 int main()
16 {
17     int T;
18     scanf("%d", &T);
19     while(T--){
20         scanf("%d", &n);
21         tail = 0;
22         for(int i = 1; i <= n; i++){
23             scanf("%d", &a[i]);
24             _next[i] = i+1;
25             _pre[i] = i-1;
26             que[tail++] = i;
27         }
28         a[0] = 0;
29         a[n+1] = N;
30         _pre[0] = 0;
31         _next[0] = 1;
32         _pre[n+1] = N;
33         _next[n+1] = n+1;
34         int ans = n, fg = 1, ptr;
35         while(fg){
36             fg = 0;
37             head = 0;
38             ptr = 0;
39             while(head < tail){
40                 int cnt = 0, i = que[head];
41                 for(; i <= n; i = _next[i]){
42                     if(a[i] > a[_next[i]]){
43                         cnt++;
44                         fg = 1;
45                     }else break;
46                 }
47                 if(cnt){
48                     ans -= cnt+1;
49                     _next[_pre[que[head]]] = _next[i];
50                     _pre[_next[i]] = _pre[que[head]];
51                     que[ptr++] = _pre[que[head]];
52                 };
53                 while(que[head] <= i && head < tail)head++;
54             }
55             tail = ptr;
56         }
57         if(ans < 0)ans = 0;
58         printf("%d\n", ans);
59         for(int i = _next[0]; i <= n; i = _next[i]){
60             if(_next[i] != N)printf("%d ", a[i]);
61         }
62         printf("\n");
63     }
64 
65     return 0;
66 }

 

HDU6215

原文:http://www.cnblogs.com/Penn000/p/7554701.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!