PAT 1101 Quick Sort[一般上]
Posted bluebluesea
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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 (≤10?5??). Then the next line contains N distinct positive integers no larger than 10?9??. 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
题目大意:找出pivot,即其左边都小于它,右边都大于它。并且输出。数据量还挺大的。
//我的想法就是遍历,那么一定会超时的啊。所以想看看大佬的写法
代码来自:https://www.liuchuo.net/archives/1917
#include <iostream> #include <algorithm> #include <vector> #include<stdio.h> int a[100000], b[100000]; using namespace std; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(a, a + n);//进行排序 int v[100000], max = 0, cnt = 0; for (int i = 0; i < n; i++) { if(a[i] == b[i] && b[i] > max) v[cnt++] = b[i]; if (b[i] > max) max = b[i]; } printf("%d ", cnt); for(int i = 0; i < cnt; i++) { if (i != 0) printf(" "); printf("%d", v[i]); } printf(" "); return 0; }
思想:很容易证明如果一个数是pivot,那么排序之后其位置肯定是不变的,(不排之前左边都比它小,右边比它大;排完之后,也是这么个情况)
1.另外当special case主元个数为0的时候,是特殊情况。
2.如果最终不输出printf(" ");会有一个格式错误,以后需要注意。
另一种解法,因为pivot拥有的性质是比左边最大的数大,比右边最小的数小。
所以一次正向遍历,求出那些比左边最大的数大的数标记为1,反向遍历一次将那些比右边最小数小的&&被标记为1的存进pivot数组,然后倒序输出(从小到大)。
代码来自:https://blog.csdn.net/kid_lwc/article/details/54780837
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> using namespace std; const int INF = 1000000007; const int N = 100010; int a[N]; int la[N]; int flag[N]; int ans[N]; int main() { int n; cin>>n; int ma=0; for(int i=0;i<n;i++){ cin>>a[i]; ma=max(ma,a[i]); if(ma==a[i]) flag[i]=1;//表示比左边最大的大。 } int mm=INF; int cnt=0; for(int i=n-1;i>=0;i--){ mm=min(mm,a[i]); if(mm==a[i] && flag[i]==1)//相等表示最小的是它。 { ans[cnt++]=a[i];//由大到小放进去 } } cout<<cnt<<endl; for(int i=cnt-1;i>=0;i--){ cout<<ans[i]; if(i) cout<<" "; } cout<<endl; return 0; }
以上是关于PAT 1101 Quick Sort[一般上]的主要内容,如果未能解决你的问题,请参考以下文章
1101 Quick Sort (25 分)难度: 一般 / 知识点: 快排
PAT Advanced 1101 Quick Sort (25分)
1101. Quick Sort (25)快排——PAT (Advanced Level) Practise