PAT B -1045 quick sort (25 points)

Click on the link full solution summary PAT B -AC

Title:
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 elements of it into the left than the main elements of Yuan put it right. The arrangement of the N positive integers different from each other after a given division, 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 left element 3 than it is small, but its right than two hours it, so it can not be a principal component;
although 2 the right elements than it is big, but big 3 on its left than it is, so it can not be a principal component;
for similar reasons, 4 and 5 are likely to be the main yuan.
Accordingly, three elements may be Principal Component Analysis.

Input format:
Enter a given positive integer N (1 ≦ 10 in line 5 ); the second row is separated by a space N different positive integers, the number of not more than 10 . 9 .

Output format:
in the first line is the number of possible output element of the main elements; output in ascending order of these elements in the second row in the system, separated by a space, the line may not have extra space inclusive.

Sample input:

5
1 3 2 4 5

Sample output:

3
1 4 5

My code:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<sstream>
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件

int main()
{
    int N;
    scanf("%d\n",&N);
    set<int>res;
    int biggest=0;
    for(int i=0;i<N;i++)
    {
        int temp;
        cin>>temp;
        if(temp>biggest)
        {
            res.insert(temp);
            biggest=temp;
        }
        else
        {
            for(set<int>::iterator it=res.begin();it!=res.end();it++)
            {
                if(*it>temp)
                {
                    res.erase(it,res.end());
                    break;
                }
            }
        }
    }
    if(res.size()==0)
    {
        cout<<"0"<<endl<<endl;
        return 0;
    }
    cout<<res.size()<<endl;
    for(set<int>::iterator it=res.begin();it!=res.end();it++)
    {
        if(it==res.begin())cout<<*it;
        else cout<<" "<<*it;
    }
    return 0;
}

case2 been reported format error ... the original ...
Here Insert Picture Description

Published 82 original articles · won praise 1 · views 1691

Guess you like

Origin blog.csdn.net/qq_34451909/article/details/104800086