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.

作者: CAO, Peng
单位: Google
时间限制: 200 ms
内存限制: 64 MB
代码长度限制: 16 KB

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​). Then the next line contains N distinct positive integers no larger than 10​9​​. 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.

思路:快速排序的主元,左边的元素都小于它,右边的元素都大于它;因此输入数据后,遍历,预先算出对于每个数字,左边最大的数和右边最小的数,每个数字只要大于左边最大的,小于右边最小的是主元。

注意数字有9位,用long型

 #include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = ; long List[maxn],LMax[maxn],RMin[maxn]; int main(){
fill(LMax,LMax+maxn,);
fill(RMin,RMin+maxn,); int n; cin >> n;
long lm=;
for(int i=;i<n;i++){
scanf("%ld",&List[i]);
LMax[i]=lm;
if(List[i]>lm) lm=List[i];
}
long rm=;
for(int i=n-;i>=;i--){
RMin[i]=rm;
if(List[i]<rm) rm=List[i];
}
vector<long> ans;
for(int i=;i<n;i++){
if(List[i]>LMax[i]&&List[i]<RMin[i]){
ans.push_back(List[i]);
}
}
sort(ans.begin(), ans.begin()); printf("%lu\n",ans.size());
for(int i=;i<ans.size();i++){
if(i!=) printf(" ");
printf("%ld",ans[i]);
}
}

1101 Quick Sort的更多相关文章

  1. PAT甲1101 Quick Sort

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

  2. PAT 1101 Quick Sort[一般上]

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

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

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

  4. 1101. Quick Sort (25)

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

  5. PAT 甲级 1101 Quick Sort

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

  6. 1101 Quick Sort(25 分

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

  7. PAT 1101 Quick Sort

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

  8. PAT (Advanced Level) 1101. Quick Sort (25)

    树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...

  9. PAT甲题题解-1101. Quick Sort (25)-大水题

    快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...

随机推荐

  1. Linux下新建服务

    1 首先在/etc/rc.d/init.d/下添加脚本 asr_cron #!/bin/bash # $Id: rc.redhat.asterisk -- ::43Z tilghman $ # # a ...

  2. TotoiseSVN 使用参考文章

    SVN使用教程总结 http://www.cnblogs.com/armyfai/p/3985660.html TotoiseSVN的基本使用方法 http://www.cnblogs.com/xil ...

  3. Jenkins构建.net项目

    一.环境搭建 1.安装所需软件 Jenkins\JDK\GIT\VS\IIS\nginx(可选) 1.1 安装iis服务: 控制面板—>程序和功能—>启用或关闭windows功能,勾选所有 ...

  4. 体验godaddy域名转入,添加A记录,及使用dnspod的NS

    有两个域名一直放在朋友那,这个朋友是个神人,经常换电话号码,联系非常不方便. 近日将域名转入到godaddy下面了,第一次做域名转移,很是好奇. 之前域名在21.cn注册的,朋友帮我申请域名转出后,2 ...

  5. (转)JavaScript的压缩

    JavaScript的压缩 (转自)http://blog.csdn.net/ybygjy/article/details/6995435 简述 如果非常着急,这块可以跳过直接从约束条件开始也行. J ...

  6. Hadoop(一) HADOOP简介

    1. HADOOP背景介绍 1.1 什么是HADOOP HADOOP是apache旗下的一套开源软件平台 HADOOP提供的功能:利用服务器集群,根据用户的自定义业务逻辑,对海量数据进行分布式处理 H ...

  7. PHP swoole process的使用

    引入背景:假如我们每天有10000个订单生成,需要同步到仓储系统中去,以前做法是开启一个crontab去跑这些任务,但是发现总有感觉同步效率低,间隔时间都是分钟级别的. 解决方案测试:我们将同步订单的 ...

  8. numpy常见属性、创建数组

      1.几种常见numpy的属性 ndim:维度 shape:行数和列数 size:元素个数 >>> import numpy as np #导入numpy模块,np是为了使用方便的 ...

  9. 01. pt-align

    01. pt-align pt-align xxx.txt =========================================== pt-align对齐输出格式 name city a ...

  10. canvas 实现太阳系效果

    一:创建画布 <canvas width="1000" height="1000" id="solar" style="ba ...