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 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 (≤). Then the next line contains N distinct positive integers no larger than 1. 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

 Keys:

  • 快速排序

Attention:

  • 最终位置上的元素不一定都是枢轴
  • 第二行的换行符不能少(无聊-,-)

Code:

 1 /*
 2 Data: 2019-07-11 21:35:25
 3 Problem: PAT_A1101#Quick Sort
 4 AC: 46:34
 5 
 6 题目大意:
 7 给定序列,统计能够作为枢轴的元素的个数,并递增输出
 8 
 9 基本思路:
10 快排中,枢轴的位置是最终位置,
11 因此排序后遍历各元素是否在最终的位置上,
12 同时确定该元素是否满足枢轴的条件
13 */
14 #include<cstdio>
15 #include<vector>
16 #include<algorithm>
17 using namespace std;
18 
19 int main()
20 {
21 #ifdef ONLINE_JUDGE
22 #else
23     freopen("Test.txt", "r", stdin);
24 #endif // ONLINE_JUDGE
25 
26     int n,Max=0,Min=1e9+10;
27     scanf("%d", &n);
28     vector<int> s(n),m(n),lmax(n),rmin(n),ans;
29     for(int i=0; i<n; i++)
30         scanf("%d", &s[i]);
31     lmax[0]=Max;
32     for(int i=1; i<n; i++)
33     {
34         if(s[i-1] > Max)
35         {
36             lmax[i]=s[i-1];
37             Max = s[i-1];
38         }
39         else
40             lmax[i]=Max;
41     }
42     rmin[n-1]=Min;
43     for(int i=n-2; i>=0; i--)
44     {
45         if(s[i+1] < Min)
46         {
47             rmin[i]=s[i+1];
48             Min = s[i+1];
49         }
50         else
51             rmin[i]=Min;
52     }
53     m=s;
54     sort(m.begin(),m.end());
55     for(int i=0; i<n; i++)
56         if(s[i]==m[i] && (s[i]>lmax[i] && s[i]<rmin[i]))
57             ans.push_back(s[i]);
58     printf("%d\n", ans.size());
59     for(int i=0; i<ans.size(); i++)
60         printf("%d%c", ans[i],i==ans.size()-1?'\n':' ');
61     if(ans.size()==0)
62         printf("\n");
63 
64     return 0;
65 }

 

上一篇:Qt Quick QMl学习笔记 之图片浏览器


下一篇:快速排序 python 代码实现