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

题意:

给一串序列,问有多少个数满足快排的privot,即左边的都小于他,右边的都大于他。

思路:

又想当然了....

首先我们知道快排有一个性质,每一轮排序后privot都会在最后应该在的位置上。

所以我们可以对原来的序列排个序,排好序的序列如果某个数和原来的一样,那这个数就有可能是privot。

只是有可能而已。比如序列5 1 3 2 4,虽然3的位置对了,但是他是不满足的。所以我们还应该要去找前i个数中的最大值,如果没有大于当前的,他才是真正可行的。至于后面有没有小于他的,其实是不用比较的。因为比如num[i]最终应该在i的位置,说明他是序列中第i大的,前面既然没有大于他的,说明1-i-1大的都在他前面了,后面是不会再有比他小的数了。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int n;
const int maxn = 1e5 + ;
LL num[maxn], tmp[maxn], ans[maxn]; int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%lld", &num[i]);
tmp[i] = num[i];
}
sort(tmp + , tmp + + n);
int cnt = , m = ;
for(int i = ; i <= n; i++){
if(tmp[i] == num[i] && num[i] > m){
ans[cnt++] = num[i];
}
if(num[i] > m){
m = num[i];
}
}
printf("%d\n", cnt);
for(int i = ; i < cnt; i++){
if(i)printf(" ");
printf("%lld", ans[i]);
}
printf("\n");
return ;
}
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

PAT甲1101 Quick Sort的更多相关文章

  1. PAT甲级——1101 Quick Sort (快速排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分)   ...

  2. PAT 甲级 1101 Quick Sort

    https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...

  3. PAT 1101 Quick Sort[一般上]

    1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...

  4. 【刷题-PAT】A1101 Quick Sort (25 分)

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  5. PAT 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  6. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  7. 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  8. 1101 Quick Sort(25 分

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  9. PAT甲级——A1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

随机推荐

  1. 7 -- Spring的基本用法 -- 7... 创建Bean的3种方式

    7.7 创建Bean的3种方式 ① 调用构造器创建Bean. ② 调用静态工厂方法创建Bean. ③ 调用实例工厂方法创建Bean. 7.7.1 使用构造器创建Bean实例. 使用构造器来创建Bean ...

  2. Dubbo -- 系统学习 笔记 -- 示例 -- 负载均衡

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 负载均衡 在集群负载均衡时,Dubbo提供了多种均衡策略,缺省为random随机调 ...

  3. 10 -- 深入使用Spring -- 5... 实现任务的自动调度

    10.5 实现任务的自动调度 10.5.1 使用Quartz 10.5.2 在Spring中使用Quartz

  4. 8 -- 深入使用Spring -- 7...3 让Spring管理控制器

    8.7.3 让Spring管理控制器 让Spring容器来管理应用中的控制器,可以充分利用Spring的IoC特性,但需要将配置Struts 2 的控制器部署在Spring容器中,因此导致配置文件冗余 ...

  5. 统计js数组中奇数元素的个数

    如何统计一个JS数组中奇数元素的个数呢? 这是群友提出的一个问题,大部分群友给出的是遍历 然后对2取模,得到最终结果. 这样的写法是最容易想得到的,那么有没有其他思路呢? 这里我提供另外一种思路,我们 ...

  6. 采用get方式提交数据到服务器实例

    GetDemo项目目录 一.编写StreamTools.java /** * */ package com.hyzhou.getdemo.utiils; import java.io.ByteArra ...

  7. Kafka(二)-- 安装配置

    一.单机部署 1.下载kafka,可在apache官网上下载,kafka要和JDK版本对应,我使用的是JDK1.7,kafka为0.10 2.进入到 /usr/java 中,解压到 此文件夹中 tar ...

  8. Django SimpleCMDB 使用序列化

    如下,前面我们是使用 urllib 方法来转换并传递数据的: [root@localhost ~]$ tail /data/script/getHostInfo.py if __name__ == ' ...

  9. Fragment获取Activity,Activity获取Fragment

    在界面布局文件中使用<fragment>元素添加Fragment时,可以为<fragment>元素指定android:id或android:tag属性,这两个属性都可用于标识该 ...

  10. WF的初步学习与创建

    一直在好奇WF的学习,嘿嘿,今天就不用啦,我之后就要接触WF的项目,刚开始在百度上寻找WF新建一个项目的过程,发现很少这样的实例让我学习操作,我想给我一个大的项目我也不可能一下就知道应该怎样去操作,由 ...