PAT B 1045 ----- quick sort (25 points)

1045 Quick Sort (25 points)

 

 

Sample input:

5
1 3 2 4 5
 

Sample output:

3
1 4 5

Ideas:
1. First principal component might find, given the array will be sorted and then compare the initial array, find the principal component
2. all possible principal component, from left to right the initial iterate over the array to find the maximum value, the left main elements of all possible side maximum storage
3. the main element for all possible, again from right to left traverse to find the minimum initial array, all possible right side pivot minimum value memory
4. the main element may meet the main element is greater than the left-membered less than the maximum right minimum value, it is determined that the main element
5. test point number of the second output of the main element is 0, it is to output two wrap, or there will be an error format (i.e., that the behavior of the output element of the main space, but still have to swap line)

for the first time through the code:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3    int a[100005];
 4    int b[100005];
 5    int maybe[100005];
 6    int max[100005];
 7    int min[100005];
 8 int cmp(const void *a,const void *b){
 9     return *(int *)a-*(int *)b;
10 }
11 
12 int main(){
13    int sum;
14    scanf("%d",&sum);
15    for(int i=0;i<sum;i++)
16     {scanf("%d",&a[i]);
17       b[i]=a[i];
18     }
19     qsort(b,sum,sizeof(int),cmp);
20     int num=0;
21     for(int i=0;i<sum;i++){
22         if(a[i]==b[i]) maybe[num++]=a[i];
23     }
24     int counter=0;
25     int max_num=0;int min_num=1e9+1;
26     for(int i=0;i<sum;i++){
27         if(a[i]==maybe[counter]){
28             max[counter++]=max_num;
29         }
30         else if(a[i]>max_num) max_num=a[i];
31     }
32     counter--;
33     for(int i=sum-1;i>=0;i--){
34         if(a[i]==maybe[counter]){
35             min[counter--]=min_num;
36         }
37         else if(a[i]<min_num) min_num=a[i];
38     }
39     int num1=0;
40     for(int i=0;i<num;i++){
41         if(maybe[i]>max[i]&&maybe[i]<min[i]) num1++;
42     }
43     printf("%d\n",num1);
44     for(int i=0;i<num;i++){
45         if(maybe[i]>max[i]&&maybe[i]<min[i]) {
46             printf("%d",maybe[i]);
47             num1--;
48             if(num1>0) printf(" ");
49         }
50         
51     }
52     printf("\n");
53     return 0;
54 }
View Code

reference:

FROM:https://www.cnblogs.com/Anber82/p/11353212.html

Guess you like

Origin www.cnblogs.com/a982961222/p/12410917.html