PAT basic 1045 Quick Sort (25 points) test point 235 error causes and solutions

One, Title Description

The famous fast sorting algorithm in a classic division of the process: We usually use some method takes an element as a principal component, through the exchange, the yuan less than the main element into its left, into the big dollar than the main elements it's right. The arrangement of the N positive integers different from each other after a given division, PCA may I ask how many elements can be divided into pre-selected?

For example given N = 5 N = 5 , the arrangement is 1,3,2,4,5. then:

1 left no elements, the elements on the right than it is large, so it could be PCA;
although left element 3 than it is small, but its right than two hours it, so it can not be a principal component;
although 2 the right elements than it is big, but big 3 on its left than it is, so it can not be a principal component;
for similar reasons, 4 and 5 are likely to be the main yuan.
Accordingly, three elements may be Principal Component Analysis.

Input format:
Enter a given positive integer N (≤10 5) in the line 1; line 2 is separated by spaces N different positive integers, each of the number of no more than 109.

Output format:
in the first line is the number of possible output element of the main elements; output in ascending order of these elements in the second row in the system, separated by a space, the line may not have extra space inclusive.

Sample input:
. 5
. 1. 5. 4. 3 2

Output Sample:
. 3
. 1. 4. 5

Second, the code

//1045
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
	int n;
	long temp;
	scanf("%d", &n);
	vector<long>a;
	vector<long>b;
	vector<long>re;
	for (int i = 0; i < n; i++)
	{
		scanf("%ld", &temp);
		a.push_back(temp);
		b.push_back(temp);
	}

	sort(b.begin(), b.end());
	long max = 0;
	for (int i = 0; i < n; i++)
	{
		/*if (a[i] == b[i])
			if(re.size()==0 || re[re.size() - 1] != a[i])
				re.push_back(a[i]);
		测试点2 3 5 错误,如果左右并不符合,但碰巧位置相同是不行的
		*/
		if (a[i] == b[i] && a[i] > max)
			re.push_back(a[i]);
		if (a[i] > max)
			max = a[i];
	}

	cout << re.size() << endl;
	if (re.size() != 0)
		printf("%ld", *re.begin());
	if(re.size() > 1 )
		for (auto it = ++re.begin(); it != re.end(); it++)
		{
			printf(" %ld", *it);
		}
	cout << endl;
	return 0;
}

Third, the test point the wrong reasons and explanations

  • About timeout problems:
    When I started the practice of double circulation, were compared, however, time out. This can also be inferred from the size of the sample through the input array is actually great, so if using a dual circulation will timeout.
  • About 235 test points
    Test point format of Question 2 Many bloggers are mentioned in the article, you need to output a single blank line
    test point 235 will examine a problem this is the first time I did not take into account, that is, by chance the same position, I found out if they meet the correct order, then the position should be the same, taking into account that if the same multiple numbers should not be counted more than once, but ignoredAbout an equal number of case numbers do not meet the

Such as input:
. 5
. 5. 4. 3 2. 1
Error Output:
. 1
. 3

Therefore, the addition of records left maximum, passed the

Collection title

Here Yo ~

Published 80 original articles · won praise 2 · Views 1971

Guess you like

Origin blog.csdn.net/qq_44352065/article/details/104613123