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 (≤). Then the next line contains N distinct positive integers no larger than 1. 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
 #include <iostream>
#include <vector>
#include <queue>
using namespace std;
int N, nums[], minN[], maxN[], res = , resNum[];//左边最大值(包括自己),右边最小值
int main()
{
cin >> N;
for (int i = ; i < N; ++i)
cin >> nums[i];
for (int i = ; i < N; ++i)//找到每个位置左边最大的值,不包括自己
maxN[i] = max(maxN[i - ], nums[i - ]);
minN[N - ] = ;
for (int i = N-; i >= ; --i)//找到每个位置右边最小的值,不包括自己
minN[i] = min(minN[i + ], nums[i + ]);
for (int i = ; i < N; ++i)
if (nums[i] > maxN[i] && nums[i] < minN[i])
resNum[res++] = nums[i];
cout << res << endl;
for (int i = ; i < res; ++i)
cout << resNum[i] << (i == res - ? "" : " ");
cout << endl;
return ;
}

PAT甲级——A1101 Quick Sort的更多相关文章

  1. PAT 甲级 1101 Quick Sort

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

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

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

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

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

  4. A1101. Quick Sort

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

  5. PAT甲1101 Quick Sort

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

  6. A1101 Quick Sort (25 分)

    一.技术总结 这里的一个关键就是理解调换位置排序是时,如果是元主,那么它要确保的条件就只有两个一个是,自己的位置不变,还有就是前面的元素不能有比自己大的. 二.参考代码 #include<ios ...

  7. 【PAT甲级】1067 Sort with Swap(0, i) (25 分)

    题意: 输入一个正整数N(<=100000),接着输入N个正整数(0~N-1的排列).每次操作可以将0和另一个数的位置进行交换,输出最少操作次数使得排列为升序. AAAAAccepted cod ...

  8. PAT_A1101#Quick Sort

    Source: PAT A1101 Quick Sort (25 分) Description: There is a classical process named partition in the ...

  9. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

随机推荐

  1. 7.springboot+mybatis+redis整合

    选择生成的依赖 选择保存的工程路径 查询已经生成的依赖,并修改mysql的版本 <dependencies> <dependency> <groupId>org.s ...

  2. C++类成员变量多用指针不用对象

    如A类的成员变量含有B类的对象,那么每个A类对象产生或拷贝都要产生一次B类对象的构造或者拷贝,对象占的空间比较大,对象拷贝比较消耗内存. 如果换成B类的指针,A类对象拷贝,也只会产生4个字节或者8个字 ...

  3. Ansible的copy模块批量下发文件

    copy模块的参数,ansible 主机组 -m copy -a '' src: 指定源文件或目录 dest: 指定目标服务器的文件或目录 backup: 是否要备份 owner: 拷贝到目标服务器后 ...

  4. 和Excel函数date同样功能的VBA函数DateSerial用法

    Sub 日期别()On Error Resume Nextlastrow = Sheets("运营日报").Range("a1048576").End(xlUp ...

  5. split的用法

    split用法返回的是数组 使用split('')根据空格返回数组 使用split()返回一个完整的数组 使用split("",3)返回前三项,是单个的字母 不过要注意: 使用sp ...

  6. 使用line-height垂直居中在安卓手机上效果不好

    前端实现单行垂直居中用的最多的方法可能就是line-height了吧.该属性在pc端和ios手机上效果都很好,可到了安卓手机,有很大几率发生文字上移的现象 知乎有人分析了导致这一现象的原因,Andro ...

  7. 【JZOJ6357】小ω的图(graph)

    description analysis 拆位从高位到低位贪心 对于当前位,如果把所有当前位为\(1\)的边塞入,\(1\)和\(n\)连通,则该位必须为\(1\) 这个是因为高位的\(1\)比所有低 ...

  8. 解析Spring第二天

    目的:使用spring中纯注解的方式 前言:同样是使用idea创建一个普通的maven工程(如何创建一个普通的Maven工程可以参考mybatis入门第一天的详解). bean管理类常用的4个注解(作 ...

  9. Html标签学习笔记二

    1.常用标签 <a></a>超链接    功能    做链接 :在href属性里面写明指向的地方        做下载:href指向文件(注意:不能下载的文件是因为浏览器可以直 ...

  10. shell脚本练习03--字符串

    ######################################################################### # File Name: -.sh # Author ...