Quick sort + dynamic planning + output details 1101 Quick Sort (25 points)

1101 Quick Sort (25 points)

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

Problem-solving
Given N numbers, determine which numbers are smaller on the left and larger on the right, and output in descending order;
because N is 10 ^ 5 at the maximum, it must time out when traversing each time , So an algorithm with complexity below nlogn is needed;
dynamic programming-space for time;
creating an array to store the largest number on the left of i;
creating an array to hold the smallest number on the right of
i ; traversing i numbers, if the largest on the left is less than i, the smallest on the right If it is greater than i, then i is added to the result array; the result array is
finally sorted and output is sufficient;

Points to note
If the result array is empty, you need to output 0 line feed, and then output line feed;
MAXL is initialized to the first number, MINI is initialized to the Nth number;
MAXL is initialized to -1, MINR is initialized to 10e + 9 also;

1. Input function

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

//给N个数

//动态规划,记录i和i之前有几个pivot
//maxpivot记录左边最大的pivot

//从右到左遍历一遍
const int MAXN = 100010;
int N;
int NUM[MAXN];
int Maxleft[MAXN];      //从左边开始最大的i 
int Minright[MAXN];     //从右边开始最大的i 

void input(){
	cin>>N;
	for(int i=0;i<N;i++)
	{
		cin>>NUM[i];
	} 
} 

2. Find pivot function

vector<int> pivot;
void Findpivot()
{
	int MAXL=NUM[0];
	int MINR=NUM[N-1];
	for(int i=0;i<N;i++)
	{
		if(MAXL<NUM[i])
			MAXL=NUM[i];
		
		Maxleft[i]=MAXL;
	}
	
	for(int i=N-1;i>=0;i--)
	{
		if(MINR>NUM[i])
			MINR=NUM[i];
		
		Minright[i]=MINR;
	}
	
	for(int i=0;i<N;i++)
	{
		if(Maxleft[i]==NUM[i]&&Minright[i]==NUM[i])
			pivot.push_back(NUM[i]);
	}

	cout<<pivot.size()<<endl;
	sort(pivot.begin(),pivot.end());
	if(pivot.size()==0) return;
	for(int i=0;i<pivot.size()-1;i++)
		cout<<pivot[i]<<" ";
	cout<<pivot[pivot.size()-1];
}

3.main function

int main()
{
	input();
	Findpivot();
}
Published 105 original articles · won praise 6 · views 4934

Guess you like

Origin blog.csdn.net/BLUEsang/article/details/105655210