A1101 Quick Sort (25 point(s))

快速排序

1. 原文

原题

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:

1
2
5
1 3 2 4 5

Sample output:

1
2
3
1 4 5

2. 解析

快速排序的每个pivot最终位置是确定的。

设一个数组,从小到大排序。

对比原数组,值相等的便是pivot,要保证值是持续增大的

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<cstdio>
#include<algorithm>
using namespace std;
int num[100010]={};
int temp[100010]={};
int ans[100010]={};
int main()
{
int n;
scanf("%d",&n);
for (int i = 0; i < n; ++i)
{
scanf("%d",&num[i]);
temp[i]=num[i];
}
sort(num,num+n);
int max=-1;
int cnt=0;
for (int i = 0; i < n; ++i)
{
if (num[i]==temp[i]&&temp[i]>max)
{
ans[cnt++]=num[i];
}
if (temp[i]>max)
{
max=temp[i];
}
}
printf("%d\n",cnt);
for (int i = 0; i < cnt; ++i)
{
if (i!=0)
{
printf(" ");
}
printf("%d",ans[i]);
}
printf("\n");
return 0;
}

:red_circle:测试点2 格式错误 why?

1
2
3
4
5
6
7
8
9
10
for (int i = 0; i < cnt; ++i)
{
printf("%d", ans[i]);
if (i<cnt-1)
{
printf(" ");
}else{
printf("\n");
}
}

way2: 递推

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<cstdio>
#include<algorithm>
using namespace std;
int origin[100010]={};
//左边最大值
int leftMax[100010]={};
//右边最小值
int rightMin[100010]={};
int ans[100010]={};
int main(){
int n;
scanf("%d",&n);
for (int i = 0; i < n; ++i)
{
scanf("%d",&origin[i]);
}
leftMax[0]=0;
for (int i = 1; i < n; ++i)
{
leftMax[i]=max(leftMax[i-1],origin[i-1]);
}
rightMin[n-1]=1<<30;
for (int i = n-2; i >= 0; i--)
{
rightMin[i]=min(rightMin[i+1],origin[i+1]);
}
int idx = 0;
for (int i = 0; i < n; ++i)
{
if (leftMax[i]<origin[i]&&rightMin[i]>origin[i])
{
ans[idx++]=origin[i];
}
}
printf("%d\n",idx);
for (int i = 0; i < idx; ++i)
{
if(i!=0){
printf(" ");
}
printf("%d",ans[i]);
}
printf("\n");
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1101/
  • 发布时间: 2019-09-03 12:30
  • 更新时间: 2021-10-29 14:07
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!