1101 Quick Sort (25 分) st表

Posted Keep--Silent

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1101 Quick Sort (25 分) st表相关的知识,希望对你有一定的参考价值。

题目链接
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

题目大意:
找出所有的x
x满足:大于左边所有的数&&小于右边所有的数

思路:
x大于左边所有的数->x大于左边的最大数
x小于右边所有的数->x小于右边的最小数
那我们可以用两个数组,一个存放左边最大数,一个存放右边最小数。
st表不太熟,干脆这题就用st表做,再熟悉一下。

#include <bits/stdc++.h>
using namespace std;

#define SIZE (100000+10)
int num1[SIZE][32];
int num2[SIZE][32];
int num[SIZE];
vector<int>ans;
int getMax(int l, int r) {
	if (l > r)return -1;
	int mid = log2(r - l + 1);
	return max(num1[l][mid], num1[r + 1 - (1 << mid)][mid]);
}
int getMin(int l, int r) {
	if (l > r)return 1e9 + 1;
	int mid = log2(r - l + 1);
	return min(num2[l][mid], num2[r + 1 - (1 << mid)][mid]);
}
int main() {
	int n, i, x,j;
	cin >> n;
	for (i = 1; i <= n; i++) {
		scanf("%d", &num[i]);
		num2[i][0] = num1[i][0]= num[i];
	}
	int to = (int)log2(n);
	for (j = 1; j <= to; j++) {
		for (i = 1;i+(1<<j)<=n+1; i++) {
			num1[i][j] = max(num1[i][j - 1], num1[i + (1 << (j - 1))][j - 1]);
			num2[i][j] = min(num2[i][j - 1], num2[i + (1 << (j - 1))][j - 1]);
		}
	}
	for (i = 1; i <= n; i++) {
		if (num[i] > getMax(1, i - 1) && num[i] < getMin(i + 1, n)) {
			ans.push_back(num[i]);
		}
	}
	sort(ans.begin(), ans.end());
	printf("%d\\n", ans.size());
    if (ans.size() == 0)cout << endl;
	for (i = 0; i < ans.size(); i++) {
		printf("%d%s", ans[i], i == ans.size() - 1 ? "\\n" : " ");
	}
	return 0;
}

以上是关于1101 Quick Sort (25 分) st表的主要内容,如果未能解决你的问题,请参考以下文章

1101 Quick Sort (25 分)递推

1101 Quick Sort (25分)

1101 Quick Sort (25分)

PAT Advanced 1101 Quick Sort (25分)

A1101 Quick Sort (25 分)

1101 Quick Sort (25 分)难度: 一般 / 知识点: 快排