PAT甲级——A1101 Quick Sort
Posted zzw1024
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲级——A1101 Quick Sort相关的知识,希望对你有一定的参考价值。
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
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 using namespace std; 5 int N, nums[100100], minN[100100], maxN[100100], res = 0, resNum[100100];//左边最大值(包括自己),右边最小值 6 int main() 7 8 cin >> N; 9 for (int i = 0; i < N; ++i) 10 cin >> nums[i]; 11 for (int i = 1; i < N; ++i)//找到每个位置左边最大的值,不包括自己 12 maxN[i] = max(maxN[i - 1], nums[i - 1]); 13 minN[N - 1] = 9999999999; 14 for (int i = N-2; i >= 0; --i)//找到每个位置右边最小的值,不包括自己 15 minN[i] = min(minN[i + 1], nums[i + 1]); 16 for (int i = 0; i < N; ++i) 17 if (nums[i] > maxN[i] && nums[i] < minN[i]) 18 resNum[res++] = nums[i]; 19 cout << res << endl; 20 for (int i = 0; i < res; ++i) 21 cout << resNum[i] << (i == res - 1 ? "" : " "); 22 cout << endl; 23 return 0; 24
以上是关于PAT甲级——A1101 Quick Sort的主要内容,如果未能解决你的问题,请参考以下文章