33,010
社区成员
发帖
与我相关
我的任务
分享
著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边。 给定划分后的N个互不相同的正整数的排列,请问有多少个元素可能是划分前选取的主元?
例如给定N = 5, 排列是1、3、2、4、5。则:
1的左边没有元素,右边的元素都比它大,所以它可能是主元;
尽管3的左边元素都比它小,但是它右边的2它小,所以它不能是主元;
尽管2的右边元素都比它大,但其左边的3比它大,所以它不能是主元;
类似原因,4和5都可能是主元。
因此,有3个元素可能是主元。
输入格式:
输入在第1行中给出一个正整数N(<= 105); 第2行是空格分隔的N个不同的正整数,每个数不超过109。
输出格式:
在第1行中输出有可能是主元的元素个数;在第2行中按递增顺序输出这些元素,其间以1个空格分隔,行末不得有多余空格。
输入样例:
5
1 3 2 4 5
输出样例:
3
1 4 5
#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int arr[100000];
unsigned int size = 0;
int arrA[100000];
int count=0;
fstream cin("in.txt");
cin>>size;
for(unsigned int i=0;i<size;i++)
{
cin>>arr[i];
}
int maxFromLeft;
int minFromRight;
int current;
int min=*(min_element(arr,arr+size));
int max=*(max_element(arr,arr+size));
for(unsigned int j=0;j<size;j++)
{
current=arr[j];
/*the first number satifies the demand*/
if(j==0&¤t==min)
{
arrA[count++]=current;
}
/*the last number satifies the demand*/
else if( (j==size-1)&¤t==max)
{
arrA[count++]=current;
}
/*the middle number satifies the demand*/
else
{
maxFromLeft=*(max_element(arr,arr+j) );
minFromRight=*(min_element(arr+j+1,arr+size));
if(maxFromLeft<current&¤t<minFromRight)
arrA[count++]=current;
}/*else*/
}
cout<<count<<endl;
sort(arrA,arrA+count);
for(int i=0;i<count;i++)
{
if(i!=count-1)
cout<<arrA[i]<<" ";
else
cout<<arrA[i];
}
system("pause");
}