Source:

PAT A1101 Quick Sort (25 分)

Description:

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

Keys:

  • 快速排序

Attention:

  • 最终位置上的元素不一定都是枢轴
  • 第二行的换行符不能少(无聊-,-)

Code:

 /*
Data: 2019-08-22 19:54:11
Problem: PAT_A1101#Quick Sort
AC: 15:45 题目大意:
给定序列,统计能够作为枢轴的元素的个数,并递增输出 基本思路:
遍历两次数组,分别记录i位置左边的最大值和右边的最小值
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e5+;
int s[M],ma[M],mi[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &s[i]);
s[]=s[];
s[n+]=s[n];
int Max=s[];
for(int i=; i<=n; i++)
{
if(s[i-]>Max)
Max=s[i-];
ma[i]=Max;
}
int Min=s[n];
for(int i=n; i>=; i--)
{
if(s[i+]<Min)
Min=s[i+];
mi[i]=Min;
}
vector<int> pivot;
for(int i=; i<=n; i++)
if(s[i]>=ma[i] && s[i]<=mi[i])
pivot.push_back(s[i]);
sort(pivot.begin(),pivot.end());
printf("%d\n", pivot.size());
for(int i=; i<pivot.size(); i++)
printf("%d%c", pivot[i],i==pivot.size()-?'\n':' ');
if(!pivot.size())
printf("\n"); return ;
}

PAT_A1101#Quick Sort的更多相关文章

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

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

  2. quick sort 的简化实现

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

  3. 1101. Quick Sort (25)

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

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

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

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

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

  6. 快速排序(Quick Sort)的C语言实现

    快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤 ...

  7. Quick Sort In-place Implementation

    在线运行PHP http://www.compileonline.com/execute_php_online.php <?php function swap( &$a, &$b ...

  8. 快速排序(Quick Sort)

    快速排序是初学者比较难理解的几个算法之一,这里尽可简单化地讲解,希望能帮到大家. 快速排序基本步骤: 从数列中挑出一个元素,称为"基准"(pivot). 重新排序数列,所有元素比基 ...

  9. 经典排序算法 - 高速排序Quick sort

    经典排序算法 - 高速排序Quick sort 原理,通过一趟扫描将要排序的数据切割成独立的两部分,当中一部分的全部数据都比另外一部分的全部数据都要小,然后再按此方法对这两部分数据分别进行高速排序,整 ...

随机推荐

  1. 【git】git的内部原理

    参考文章:https://zhuanlan.zhihu.com/p/96631135 参考文章:https://marklodato.github.io/visual-git-guide/index- ...

  2. SecureCRT key登录linux ssh设置

    一.首先用secureCrt创建密钥 1.使用SecureCRT创建私钥和公钥. SecureCRT quick Connect-> Authentiation -> Public Key ...

  3. 使用postman做接口测试----柠檬不萌!

    目录 一.GET和POST请求的区别 二.http协议 1.http请求分为两个部分 2.http状态码 三.使用postman测试HTTP接口 1.请求方式:get 2.请求方式:post 3.请求 ...

  4. 现在就去100offer 参加互联网人才拍卖! 现在登录现在注册 为什么整个互联网行业都缺前端工程师?

    现在,几乎整个互联网行业都缺前端工程师,不仅在刚起步的创业公司,上市公司乃至巨头,这个问题也一样存在.没错,优秀的前端工程师简直比大熊猫还稀少. 每天,100offer的HR群都有人在吐槽招不到前端工 ...

  5. TCP建立连接的三次握手和释放连接的四次挥手

    TCP建立连接时,为什么要进行三次握手? 举个打电话的例子: A : 你好我是A,你听得到我在说话吗 B : 听到了,我是B,你听到我在说话吗 A : 嗯,听到了 建立连接,开始聊天! 第一次握手 第 ...

  6. WPF 从服务器下载文件

    1.先获取服务器下载地址,给出要下载到的目标地址 public void DownloadFileFromServer() { string serverFilePath = "http:/ ...

  7. mysql5.7 基于gtid的主从复制

    基本环境 版本 5.7.14 主库ip:192.168.1.100 port:3306 从库ip:102.168.1.101 port:3306 搭建注意事项 主库配置 gtid-mode=on en ...

  8. vue之ref

    ref 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素:如果用在子组件上,引用就指向组件. 1.ref ...

  9. Delphi如何获取一个字符串再另一个字符串中最后一次出现的位置

    uses StrUtils;   function ReversePos(SubStr, S: String): Integer; var   i : Integer; begin   i := Po ...

  10. Linux直接在通过终端打开图片文件

    为了提高效率,减少使用鼠标,有时候想直接通过终端的命令打开一个图片进行查看.可以使用的命令有: eog filename display filename 再使用Alt+F4就可以关闭窗口,尽量达到手 ...