1101. Quick Sort (25)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1101. Quick Sort (25)相关的知识,希望对你有一定的参考价值。

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 are moved to its left and those larger than the pivot to its right. Given N distinct positive integers after a run of partition, could you tell how many elements could be 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 is no element to its left and all the elements to its right are larger than it;
  • 3 must not be the pivot since although all the elements to its left are smaller, the number 2 to its right is less than it as well;
  • 2 must not be the pivot since although all the elements to its right are larger, the number 3 to its left is larger than it as well;
  • and for the similar reason, 4 and 5 could also be the pivot.

    Hence in total there are 3 pivot candidates.

    Input Specification:

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

    Output Specification:

    For each test case, output in the first line the number of pivot candidates. Then in the next line print these candidates in increasing order. There must be exactly 1 space between two 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
    
  •  

  •  1 #include<stdio.h>
     2 #include<string>
     3 #include<iostream>
     4 #include<string.h>
     5 #include<sstream>
     6 #include<vector>
     7 #include<map>
     8 using namespace std;
     9 bool is[100100];
    10 int main()
    11 {
    12     int n;
    13     int MAX = -1,MIN = 1000000000,tem;
    14     vector<int> vv,re;
    15     scanf("%d",&n);
    16     for(int i = 0;i <n;++i)
    17     {
    18         scanf("%d",&tem);
    19         if(tem > MAX)
    20         {
    21             is[i] = 1;
    22             MAX = tem;
    23         }
    24         vv.push_back(tem);
    25     }
    26     for(int i = n-1;i >=0;--i)
    27     {    
    28         if(vv[i] < MIN)
    29         {
    30             MIN = vv[i];
    31             if(is[i]) re.push_back(vv[i]);
    32         }
    33         else is[i] = 0;
    34     }
    35     printf("%d\n",re.size());
    36     for(int i = re.size() - 1;i >= 0;--i)
    37     {
    38         if(i == re.size() - 1) printf("%d",re[i]);
    39         else printf(" %d",re[i]);
    40     }
    41     printf("\n");
    42     return 0;
    43 }

     

以上是关于1101. Quick Sort (25)的主要内容,如果未能解决你的问题,请参考以下文章

1101. Quick Sort (25)快排——PAT (Advanced Level) Practise

1101. Quick Sort (25)快排——PAT (Advanced Level) Practise

A1101 Quick Sort (25 分)

1101 Quick Sort (25 分)

1101 Quick Sort (25 分)递推

1101 Quick Sort (25 分) st表