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. NFS服务器简易安装

    1.服务端 创建挂载目录 # mkdir /data/nfs 安装NFS软件 # yum install nfs-utils -y 添加配置信息 # vim /etc/exports /data/nf ...

  2. 如何通过SVN管理好代码

    来自:http://blog.csdn.net/baronyang/article/details/6942434 ------------------------------------------ ...

  3. JDBC_数据库连接池c3p0

    /** * @Description: TODO(这里用一句话描述这个类的作用) * @Author aikang * @Date 2019/8/26 20:12 */ /* 1.数据库连接池: 1. ...

  4. Jmeter压测快速体验

    前言 最近在看neo4j相关的官网文档以及一些调优参数,同时也学了下Jmeter,为了测试下neo4j服务的性能,虽然不是专业搞测试的,但是我觉得每个优秀的开发者都应该学会主动压测自己服务和代码的性能 ...

  5. Java oop第05章_多态、接口

    一. 为什么需要接口? Java中当一个类继承父类时,最多也只能继承一个父类,扩展性会受到一定的限制,为了弥补这一缺陷,Java又提供了一种新的引用数据类型分类,叫接口. 一个类只能继承一个父类,但可 ...

  6. [JZOJ6340] 【NOIP2019模拟2019.9.4】B

    题目 题目大意 给你个非负整数数列\(a\),每次等概率选择大于零的\(a_i\),使其减\(1\). 问\(a_1\)被减到\(0\)的时候期望经过多少次操作. 思考历程 对于这题的暴力做法,显然可 ...

  7. thinkphp for标签

    用法: 大理石平台厂家 <for start="开始值" end="结束值" comparison="" step="步进值 ...

  8. mac 10.9 install cocoapods issue

    If you've installed the OS X Mavericks Beta and you're having ruby issues like this: /System/Library ...

  9. System.Web.HttpSessionStateBase.cs

    ylbtech-System.Web.HttpSessionStateBase.cs 1.程序集 System.Web, Version=4.0.0.0, Culture=neutral, Publi ...

  10. datagrid 的标题的内容不对应整齐

    $(document).ready(function(){ var column = "[["+ "{'title':'工号','field':'grantorCode' ...