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. IK的整个分词处理过程

    首先,介绍一下IK的整个分词处理过程: 1. Lucene的分词基类是Analyzer,所以IK提供了Analyzer的一个实现类IKAnalyzer.首先,我们要实例化一个IKAnalyzer,它有 ...

  2. Thread-per-Message 这个工作交给你了

    Per是“每一”的意思,所以thread per message解释过来就是“每个消息一个线程”,message在这里可以看做是“命令”或“请求”的意思,对每隔命令或请求,分配一个线程,有这个线程执行 ...

  3. RAKsmart服务器受消费者青睐的原因

    随着互联网的快速发展,网站建设变得越来越重要,现在很多做外贸网站的都会选择美国服务器来建设网站,近年来RAKsmart服务器受到广大站长的欢迎,那RAKsmart服务器受消费者青睐的关键是什么呢. 1 ...

  4. 2019.12.04 Java中的内存分配

    Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Java的内存管理与内存泄露以及Java垃圾回收方面的知识,今天我们再次深入Java核心,详细介绍一下Java在内存分配方面的知识.一般 ...

  5. 记一次付工解决Sqlserver问题的过程

    问题:Sqlserver连接不上 1 检查自身 Sqlserve服务是否开启 Windows服务是否开启 2 用CMD尝试连接Sqlserver 显示详细错误 由于文件不可访问,或者内存或磁盘空间不 ...

  6. AIO 详解

    AIO(Asynchronous Input and Output) 异步IO则采用"订阅-通知"模式: 即应用程序向操作系统注册IO监听,然后继续做自己的事情. 当操作系统发生I ...

  7. Bootstrap FileInput.js + springmvc uploadFile

    下载FileInput.js          http://plugins.krajee.com/file-input/demo     https://github.com/kartik-v/bo ...

  8. SPSS分类分析:决策树

    SPSS分类分析:决策树 一.决策树(分析-分类-决策树) "决策树"过程创建基于树的分类模型.它将个案分为若干组,或根据自变量(预测变量)的值预测因变量(目标变量)的值.此过程为 ...

  9. day25-静态、组合、继承

    #!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...

  10. day 72 Django基础七之Ajax

    Django基础七之Ajax   本节目录 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) 六 同源策略与 ...