PAT Basic 1045 Quick Sort (25 points)

The famous fast sorting algorithm in a classic division of the process: We usually use some method takes an element as a principal component, through the exchange, the yuan less than the main element into its left, into the big dollar than the main elements it's right. Given to the division of  N permutations of positive integers different from each other, PCA may I ask how many elements can be divided into pre-selected?

For example given $ N = 5 $, is arranged 1,3,2,4,5. then:

  • 1 left no elements, the elements on the right than it is large, so it could be PCA;
  • Although the left element 3 than it is small, but its right 2 is smaller than it, so it can not be a principal component;
  • Although the right elements 2 than it is big, but it left three larger than it, so it can not be a principal component;
  • For similar reasons, 4 and 5 are likely to be the main element.

Accordingly, three elements may be Principal Component Analysis.

Input formats:

Gives a positive integer in the input line 1  N ( ≤); second line is separated by a space  N different positive integers, each number is not more than  1.

Output formats:

In line 1, the output may be the number of elements of the main element; these elements are output in ascending order in the second row, separated by a space therebetween, end to end row may not have extra space.

Sample input:

5
1 3 2 4 5

Sample output:

3
1 4 5

#include <the iostream> 
#include <algorithm> 
#include <Vector>
 the using  namespace STD;
 / * * Thought: establishing a guarantee of the maximum left, right, and also the position of the array in the sorted * / 
int main () 
{ 
    int N; Scanf ( " % D " , & N); 
    Vector < int > ARR (N), sort_arr (N); 
    Vector < int > V;
     for ( int I = 0 ; I <N; I ++ ) { 
        Scanf ( " % D " , & ARR [I]); // readings
        sort_arr[i]=arr[i];//拷贝
    }
    sort(sort_arr.begin(),sort_arr.end());//sort arr是有序序列
    int max_num=arr[0];
    for(int i=0;i<N;i++){
        if(arr[i]==sort_arr[i]&&max_num==arr[i]) v.push_back(arr[i]);
        max_num=max(max_num,arr[i+1]);
    }
    cout<<v.size()<<endl;
    for(int i=0;i<v.size();i++)
        if(i!=v.size()-1) printf("%d ",v[i]);
        else printf("%d",v[i]);
    cout<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11706443.html