原题说明:
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
(
<
=
1
0
5
)
N (<=10^5)
N(<=105). Then the next line contains
N
N
N distinct positive integers no larger than
1
0
9
10^9
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
题目大意:
寻找快速排序中可取的pivot(在序列中,左边的数均不大于pivot,右边的数均不大于pivot),打印pivot的个数和可选pivot的递增序列. 这里采取线段树解决,效率并非最优,仅提供一个解决思路.
参考代码:
(* 注意:测试点3,即不存在pivot的测试样例存在问题,需要输出0后单独再输出一个空行.)
#include <bits/stdc++.h>
using namespace std;
int const MaxN = 100000, maxValue = 0x3F3F3F3F;
int arr[MaxN];
pair<int, int> tree[MaxN << 2];
void buildTree(int node, int start, int end) {
if (start == end) {
tree[node] = {arr[start], arr[end]};
return;
}
int mid = start + (end - start) / 2;
int leftNode = node * 2 + 1, rightNode = node * 2 + 2;
buildTree(leftNode, start, mid);
buildTree(rightNode, mid + 1, end);
tree[node] = {min(tree[leftNode].first, tree[rightNode].first), max(tree[leftNode].second, tree[rightNode].second)};
}
pair<int, int> query(int node, int start, int end, int left, int right) {
if (right < start || end < left)
return {maxValue, -maxValue};
if (left <= start && end <= right)
return tree[node];
if (start == end)
return tree[node];
int mid = start + (end - start) / 2;
auto leftPart = query(node * 2 + 1, start, mid, left, right);
auto rightPart = query(node * 2 + 2, mid + 1, end, left, right);
return {min(leftPart.first, rightPart.first), max(leftPart.second, rightPart.second)};
}
int main(void) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
buildTree(0, 0, n - 1);
vector<int> list;
for (int i = 0; i < n; i++) {
if (query(0, 0, n - 1, 0, i - 1).second <= arr[i] && query(0, 0, n - 1, i + 1, n - 1).first >= arr[i])
list.push_back(arr[i]);
}
printf("%d\n", list.size());
if (list.size() == 0)
printf("\n");
for (int i = 0; i < list.size(); i++)
printf(i == list.size() - 1 ? "%d" : "%d ", list[i]);
return 0;
}
附:提交记录: