PAT甲级刷题记录——1101 Quick Sort (25分)

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

思路

这题的题目意思很简单哈,但是做起来并没有那么容易。

题目大意就是:给你一串序列,让你判断其中的每个数是不是能成为主元(也就是它的左边是不是都比它小,它的右边是不是都比它大,如果这两个条件满足,那么它就是一个主元)。最后第一行输出能成为主元的个数,第二行升序输出每个主元。

我这里借鉴了柳神的思路,通过排序的方法来判断每个数是不是主元。这里要注意的是,如果仅仅通过排序前后位置相同来判断是否是主元的话,是不正确的。比如:5 2 3 4 1,排完序之后是:1 2 3 4 5,3的位置依然没变,但是它显然不应该成为主元,因此,我们还要加一个判断条件:当前数在原序列中,它左边的最大值是不是比它还小(判断右边的最小值是不是比它还大也行,因为如果出现不符合的情况,肯定是对称的,所以只要判断一边即可)。如果它左边的最大值比它还小,并且排完序之后它所处的位置依然不变的话,那么就能确定这是一个主元啦~

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<map>
#include<vector>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 100010;
int data[maxn] = {0};
int left_max[maxn] = {0};
map<int, int> pos;
vector<int> result;
int maxData = -1;
int main()
{
    int N;
    scanf("%d", &N);
    for(int i=0;i<N;i++){
        scanf("%d", &data[i]);
        pos[data[i]] = i;
        left_max[i] = maxData;//记录i左边的最大值
        if(data[i]>maxData) maxData = data[i];
    }
    sort(data, data+N);
    for(int i=0;i<N;i++){
        if(pos[data[i]]==i&&left_max[i]<data[i]) result.push_back(data[i]);
        //如果排完序之后位置不变,并且原序列中它左边的最大值比它还小,就说明是一个主元
    }
    sort(result.begin(), result.end());
    printf("%d\n", result.size());
    if(result.size()==0) printf("\n");
    for(int i=0;i<result.size();i++){
        if(i==0) printf("%d", result[i]);
        else printf(" %d", result[i]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值