1101. Quick sort (25) "Quick-row"--pat (Advanced level) practise

Source: Internet
Author: User

Topic Information

1101. Quick Sort (25)

Time limit of MS
Memory Limit 65536 KB
Code length limit 16000 B
There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot is moved to their left and those larger than the pivot to their right. Given N distinct positive integers after a run of partition, could-how many elements could is the selected pivot For this partition?

For example, given N = 5 and the numbers 1, 3, 2, 4, and 5. We have:

1 could be the pivot since there was no element to its left and all of the elements to their right was larger than it;
3 must not being the pivot since although all the elements-to-it left was smaller, the number 2 to their right was less than it as well;
2 must not being the pivot since although all the elements to their right was larger, the number 3 to the It is larger than I T as well;
And for the similar reason, 4 and 5 could also is the pivot.
Hence in total there is 3 pivot candidates.

Input Specification:

Each input file contains the one test case. For each case, the first line gives a positive integer N (<= 10^5). Then the next line contains N distinct positive integers no larger than 10^9. The numbers in a line is separated by spaces.

Output Specification:

For each test case, output in the first line the number of pivot candidates. Then on the next line print these candidates in increasing order. There must be exactly 1 space between a adjacent numbers, and no extra space at the end of each line.

Sample Input:
5
1 3 2) 4 5
Sample Output:
3
1 4 5

Thinking of solving problems

Sort

AC Code
#include <cstdio>#include <algorithm>#include <vector>using namespace STD;intN, mx[100005], RMN, a[100005];intMain () {scanf("%d", &n); mx[0] =0; RMN =0x7fffffff; for(inti =1; I <= N; ++i) {scanf("%d", a+i); Mx[i] = max (mx[i-1], a[i]); } vector<int>V for(inti = n; I >=1; -I.) {if(A[i] >= mx[i-1] && A[i] <= rmn) {v.push_back (a[i]);    } RMN = min (RMN, a[i]); }printf("%d\n", V.size ()); Sort (V.begin (), V.end ());if(V.size () >0) {printf("%d", v[0]); } for(inti =1; I < v.size (); ++i) {printf("%d", V[i]); }printf("\ n");return 0;}

1101. Quick sort (25) "Quick-row"--pat (Advanced level) practise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.