1101 Quick Sort (25 point(s))
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
题目大意
给定一串数组,要求你输出有多少个数字能成为哨兵以及它们的值。此处的哨兵意为其左边的值均小于它,右边的值均大于它
解题思路
本题一开始引入了快排的概念,但其实跟快排没什么关系。
我开始利用了两个数组L,R分别存储i位置左边比arr[i]小的个数和i位置右边比arr[i]大的个数,再判断其每个位置L[i]+R[i]==N-1,如果满足,则说明该位置的数可以成为哨兵,否则不是哨兵。详见代码1
随后阅读了算法笔记上的代码,也是建立两个数组leftMax, rightMin,但分别存储i位置左边的最大数以及i位置右边的最小数,再判断arr[i] > leftMax[i] && arr[i] < right[i],如果满足说明该位置是哨兵,否则不是。这种方法代码似乎简洁一些,详见代码2
代码1
//法1,判断某个数左边比它小的个数+右边比它大的个数是否等于N-1
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
const int maxn = 100010;
int arr[maxn] = { 0 };
int L[maxn] = { 0 }, R[maxn] = { 0 };//分别表示左边比arr[i]小的数的个数和右边比arr[i]大的数的个数
int main()
{
int N, i, cnt = 0;
int ret = scanf("%d", &N);
int tmp = -1;
for (i = 0; i < N; ++i) {
ret = scanf("%d", &arr[i]);
if (i) {
if (arr[i] > tmp) {//处于递增数列才可能成为哨兵
L[i] = i;
tmp = arr[i];
}
}
}
tmp = 10000000000;//无穷大
for (i = N - 1; i >= 0; --i) {
if (arr[i] < tmp) {//从右遍历,处于递减数列才可能成为哨兵
R[i] = N - 1 - i;
tmp = arr[i];
}
}
for (i = 0; i < N; ++i) {//得到总个数
if (L[i] + R[i] == N - 1) {//若arr[i]左边的数都比arr[i]小,右边的数都比arr[i]大
++cnt;
}
}
printf("%d\n", cnt);
int flag = 0;
for (i = 0; i < N; ++i) {
if (L[i] + R[i] == N - 1) {
if (flag) printf(" ");
printf("%d", arr[i]);
flag = 1;
}
}
printf("\n");//必须输出空行
return 0;
}
代码2
//法2,判断某数左边的最大数是否比它小以及右边的最小数是否比它大
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100010;
int arr[maxn], leftMax[maxn], rightMin[maxn], res[maxn];//分别存储原始数据,i位置左边的最大数,i位置
//右边的最小数以及最终结果
int main()
{
int N, i, cnt = 0;
int ret = scanf("%d", &N);
for (i = 0; i < N; ++i) {
ret = scanf("%d", &arr[i]);
}
leftMax[0] = 0;
for (i = 1; i < N; ++i) {
leftMax[i] = max(leftMax[i - 1], arr[i - 1]);
}
rightMin[N - 1] = 1000000005;
for (i = N - 2; i >= 0; --i) {
rightMin[i] = min(rightMin[i + 1], arr[i + 1]);
}
for (i = 0; i < N; ++i) {
if (arr[i] > leftMax[i] && arr[i] < rightMin[i]) {//如果i左边的最大数小于arr[i],右边的最小数
res[cnt++] = arr[i]; //大于arr[i]
}
}
printf("%d\n", cnt);
for (i = 0; i < cnt; ++i) {
if (i) printf(" ");
printf("%d", res[i]);
}
printf("\n");//必须输出空行
return 0;
}