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 }
时间: 2024-10-12 19:34:27

1101. Quick Sort (25)的相关文章

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

题目信息 1101. Quick Sort (25) 时间限制200 ms 内存限制65536 kB 代码长度限制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 are

PAT甲题题解-1101. Quick Sort (25)-大水题

快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着循环一次,统计出代码中的minnum和maxnum即可,注意最后一定要输出'\n',不然第三个测试会显示PE,格式错误. #include <iostream> #include <cstdio> #include <algorithm> #include <map&

PAT (Advanced Level) 1101. Quick Sort (25)

树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #include<algorithm> using namespace std; const int maxn=100000+10; int a[maxn],ans[maxn],c[maxn],b[maxn]; int n; map<int,int>m; int lowbit(int x)

【PAT甲级】1101 Quick Sort (25 分)

题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次??.....这是为何 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[100007]; 5 int b[100007]; 6 int

pat1101. Quick Sort (25)

1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng 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

1101 Quick Sort (25 分)递推

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 th

PAT 1101 Quick Sort[一般上]

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

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. G

PAT_A1101#Quick Sort

Source: PAT A1101 Quick Sort (25 分) Description: 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 lef