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. 在ALV界面添加下拉框

    1.在alv界面表单属性中设置 if wa_fieldcat-fieldname = 'YC_MARK'.              wa_fieldcat-edit = 'X'.           ...

  2. vue路由权限之访问权限(meta控制是否有访问权限)

    首先登录那权限表 router.beforeEach((to, from, next) => { if(to.path === '/login') { next(); }else{ if(!st ...

  3. Bonding

    一.简介 双网卡配置设置虚拟为一个网卡实现网卡的冗余,其中一个网卡坏掉后网络通信仍可正常使用,实现网卡层面的负载均衡和高可用性   二.原理 网卡工作在混杂(promisc)模式,接收到达网卡的所有数 ...

  4. 8M - 三角形

    用N个三角形最多可以把平面分成几个区域?  Input 输入数据的第一行是一个正整数T(1<=T<=10000),表示测试数据的数量.然后是T组测试数据,每组测试数据只包含一个正整数N(1 ...

  5. C# 在网页中将Base64编码的字符串显示成图片

    在写一个接口,返回的json里面有图片,是Base64编码的字符串. 测试接口的时候,发现原来在html显示,是直接可以将Base64编码的字符串显示成图片的. 格式如下: <img src=d ...

  6. hdu 1010(DFS) 骨头的诱惑

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意从S出发,问能否在时间t的时候到达终点D,X为障碍 需要注意的是要恰好在t时刻到达,而不是在t时间 ...

  7. IDEA 的 Edit 设置

    1.设置鼠标悬浮提示 General -- Show quick documentation on mouse move 2.自动导包 3.设置显示行号和方法的间隔符 4.忽略大小写  4.设置取消单 ...

  8. git分支切换时的时间戳问题

    1.为什么git仓库没有保留文件修改时的时间戳?  摘自:https://git.wiki.kernel.org/index.php/Git_FAQ#Why_isn.27t_Git_preservin ...

  9. mysql 优化之一

    提升速度 show  variables like 'innodb_flush_log_at_trx_commit'; 会显示为1 set global innodb_flush_log_at_trx ...

  10. Netty Reator(三)Reactor 模型

    Netty Reator(三)Reactor 模型 Netty 系列目录 (https://www.cnblogs.com/binarylei/p/10117436.html) 本文介绍 DC Sch ...