PAT A1101 Quick Sort (25 分)/B 1045 快速排序 (25 分)

PAT A1101 Quick Sort (25 分)/B 1045 快速排序 (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

B1045 快速排序 (25 分) 中文题目:
著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边。 给定划分后的 N 个互不相同的正整数的排列,请问有多少个元素可能是划分前选取的主元?

例如给定 N = 5 N = 5 N=5, 排列是1、3、2、4、5。则:

1 的左边没有元素,右边的元素都比它大,所以它可能是主元;
尽管 3 的左边元素都比它小,但其右边的 2 比它小,所以它不能是主元;
尽管 2 的右边元素都比它大,但其左边的 3 比它大,所以它不能是主元;
类似原因,4 和 5 都可能是主元。
因此,有 3 个元素可能是主元。

输入格式:
输入在第 1 行中给出一个正整数 N(≤10^​5​​ ); 第 2 行是空格分隔的 N 个不同的正整数,每个数不超过 10^​9​​ 。

输出格式:
在第 1 行中输出有可能是主元的元素个数;在第 2 行中按递增顺序输出这些元素,其间以 1 个空格分隔,行首尾不得有多余空格。

输入样例:
5
1 3 2 4 5
输出样例:
3
1 4 5

以下是AC的代码:

#include<cstdio>
#define maxn 100100 
bool ppivot[maxn]={true};
int num[maxn]={0};
int main(void){
	int total,count=0,i;
	scanf("%d",&total);
	for(i=0;i<total;i++)
		scanf("%d",&num[i]); 
	int big=num[0],small=num[total-1];
	for(i=1;i<total;i++){
		if(big>num[i]){
			ppivot[i]=false;
		}else{
			ppivot[i]=true;
			big=num[i];
		}
	}
	for(i=total-2;i>=0;i--){
		if(small<num[i]){
			ppivot[i]=false;
		}else{
			small=num[i];
		}
	}
	for(i=0;i<total;i++){
		if(ppivot[i]) count++;
	}
	printf("%d\n",count);
	bool first =true;
	if(count>0){
		for(i=0;i<total;i++){
			if(ppivot[i]){
				if(first){
					first = false;
				}else printf(" ");
				printf("%d",num[i]);
			}
		}
	}
	printf("\n");
	return 0;
}

思路:
这道题与A1093/B1040 思路非常相似,可以结合起来看。
题目让找出所给序列中可能曾被选成 主元的元素。主元的左边所有元素都小于它,右边所有元素都大于它,所以我们只要找出符合这两个要求的元素输出即可。
使用整型变量big ,small,big 中存储的是当前元素左边元素中最大的,如果最大的元素都小于当前元素,当前元素可能是主元;small 中存储的是当前元素右边元素中最小的,如果最小的元素都大于当前元素,当前元素可能是主元。使用bool数组 ppivot[] 存储当前元素是否可能是需要输出的主元,如果通过两个循环后的ppivot[i] 还是true,说明当前元素是主元,让count++,最后再依次按照ppivot输出即可。
这道题有个非常坑人的地方,就是按照题目要求这样写完了,第三个测试点无法通过并且显示“格式错误”。这里是因为,当符合要求的主元数是0的时候,必须输出 “0\n\n”,这里必须有两个 "\n"才能通过,也就是说输出必须是两行。一定要注意。

上一篇:2021年河池市中考录取分数线(河池)


下一篇:C++核心准则​NR.4:不要坚持将每个类声明放在其自己的源文件中