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 (<= 105). Then the next line contains N distinct positive integers no larger than 109. 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<cstdio>
#include<iostream>
#include<limits.h>
using namespace std;
int num[], L[] = {,}, R[] = {,};
int main(){
int N, max = -, cnt = , index = -;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d", &num[i]);
}
for(int i = ; i < N; i++){
if(num[i] > max){
max = num[i];
L[i] = ;
}
}
int min = INT_MAX;
for(int i = N - ; i >= ; i--){
if(num[i] < min){
min = num[i];
R[i] = ;
}
}
for(int i = ; i < N; i++){
if(L[i] && R[i]){
cnt++;
}
if(L[i] && R[i] && index == -){
index = i;
}
}
if(cnt == )
printf("%d\n\n", cnt);
else
printf("%d\n%d", cnt, num[index]);
for(int i = index + ; i < N; i++){
if(L[i] && R[i])
printf(" %d", num[i]);
}
cin >> N;
return ;
}

总结:

1、与上一题一样的做法。注意无解的情况,应输出2个\n,以表示输出了2行, 否则会报格式错误。

2、注意min和max的初始值,输入的数字可能很大,最好让min和max在long long的范围足够小和足够大。

A1101. Quick Sort的更多相关文章

  1. PAT甲级——A1101 Quick Sort

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

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

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

  3. A1101 Quick Sort (25 分)

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

  4. PAT_A1101#Quick Sort

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

  5. [算法]——快速排序(Quick Sort)

    顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...

  6. quick sort 的简化实现

    Pivot 随机选取意义不大 第一种方法使用随机pivot,使得尽可能平均二分序列,而实际上一般来说需要排序的集合往往是乱序的,无需重新生成随机数作为pivot,大可使用固定位置的数作为pivot,这 ...

  7. 1101. Quick Sort (25)

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

  8. [算法] 快速排序 Quick Sort

    快速排序(Quick Sort)使用分治法策略. 它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分:其中一部分的所有数据都比另外一部分的所有数据都要小.然后,再按此方法对这 ...

  9. 基础排序算法之快速排序(Quick Sort)

    快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...

随机推荐

  1. Rancher + k8s + docker 部署资料

    一.k8s 文档: https://jimmysong.io/kubernetes-handbook/concepts/deployment.html 命令行大全 https://kubernetes ...

  2. NOIP模拟赛20180917 隐藏题目

    给定n个数,值域范围1~n,每个数都不同,求满足所有相邻数不同的排列数.\(n\le 30\). 状压DP很好想,然而我考试时没写出来.对于排列还是有很多转化方法.另外要注意数据范围.

  3. 《linux内核设计与实现》第四章

    调度程序负责决定哪个进程投入运行,何时运行以及运行多长时间.只有通过调度程序合理调度,系统资源才能最大限度发挥作用,多进程才会有并发执行的效果. 最大限度地利用处理器时间的原则是,只要有可以执行的进程 ...

  4. “数学口袋精灵”App的第三个Sprint计划(总结与团队感悟)----开发日记

    第三阶段Sprint完成情况: 我们的"数学口袋精灵"App已经完成了,该app能随机产生多种形式的算式,比如带括号的,分数四则运算,混合运算,阶乘等,通过游戏形式让用户乐在其中. ...

  5. 20172319 《Java程序设计教程》第8周学习总结

    20172319 2018.04.24-05.03 <Java程序设计教程>第8周学习总结 目录 教材学习内容总结 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周考 ...

  6. pandas修改全列的时间格式 无需使用apply

    df.date.dt.strftime('%Y%m%d') #实现全列修改时间格式

  7. 学习yii2.0——基础入门

    声明:本文内容来自于yiichina.com的权威指南. 安装 推荐使用composer来安全,可能有点慢(要下载的依赖比较多). composer create-project --prefer-d ...

  8. (html4与html5分别实现)用一个表单向php界面提交不同的命令

    先看上面的表单实例,分别有3个submit,只不过需要根据需要进行选择添加.删除或是修改.现在有两个方案. 方案一: 如果实在HTML4中,可以用下面的代码实现, 即将所有的submit提交到acce ...

  9. python2.7.x的字符串编码到底什么鬼?(中文和英文的处理)

    一直以来我其实一直对python的编码弄得非常晕,能正常编码,也能处理一些情况.但是始终不明白有些问题究竟为何出,原因是什么,为什么要这样用. 今天晚上正好好好研究了一番解答了自己心中的困惑. Q:p ...

  10. loadrunner基础学习笔记二

    virtual user generator(vugen) 在测试环境中,loadrunner在物理计算机上使用vuser代替实际用户.vuser以一种可重复.可预测的方式模拟典型用户的操作,对系统施 ...