欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT 1045 快速排序

程序员文章站 2022-07-15 14:02:05
...
#include <bits/stdc++.h>
#define TEST
using namespace std;

int main()
{
	int n;
	scanf("%d", &n);

	static int input[100010];
	static int output[100010];
	static int max[100010];
	int count = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d", &input[i]);
		output[i] = input[i];
		if (i == 0)	max[i] = input[i];
		else {
			if(input[i] > max[i-1])	max[i] = input[i];
			else max[i] = max[i - 1];
		}
	}
	sort(output, output + n);
	for (int i = 0; i < n; i++) {
		if (output[i] != input[i] || output[i] != max[i])
			output[i] = 0;
		else count++;
	}
	printf("%d\n", count);
	if (count == 0)
		printf("\n");
	else {
		for (int i = 0; i < n; i++) {
			if (output[i] != 0) {
				printf("%d", output[i]);
				if (count != 1) 	printf(" ");
				count--;
			}
		}
	}

#ifdef TEST
	system("pause");
#endif
	return 0;
}