1101 Quick Sort
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.
思路:快速排序的主元,左边的元素都小于它,右边的元素都大于它;因此输入数据后,遍历,预先算出对于每个数字,左边最大的数和右边最小的数,每个数字只要大于左边最大的,小于右边最小的是主元。
注意数字有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的更多相关文章
- PAT甲1101 Quick Sort
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- PAT 1101 Quick Sort[一般上]
1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...
- PAT甲级——1101 Quick Sort (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分) ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT 甲级 1101 Quick Sort
https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...
- 1101 Quick Sort(25 分
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT 1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT (Advanced Level) 1101. Quick Sort (25)
树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...
- PAT甲题题解-1101. Quick Sort (25)-大水题
快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...
随机推荐
- ubuntu 安装Pangolin 过程
Pangolin 是一款开源的OPENGL显示库,可以用来视频显示.而且开发容易. 代码我们可以从Github 进行下载:https://github.com/zzx2GH/Pangolin.git ...
- [Z]scp example
https://www.cnblogs.com/autumnvivi/articles/3447964.html
- HTML DOM 事件对象
HTML DOM 事件对象 由 youj 创建,小路依依 最后一次修改 2016-08-04 HTML DOM 事件 HTML DOM 事件 HTML DOM 事件允许Javascript在HTML文 ...
- python 常库介绍及安装方法
bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctypes:用来调用动态链接库DBUtils:数 ...
- PHP 语句 函数 字符串处理
语句: 一:分支语句 1.if(条件1){满足条件1执行} 2.if(条件1){满足条件1执行}else{不满足条件1执行} 3.if(条件1){满足条件1执行}else if(条件2){满足条件2执 ...
- iOS.Info.plist
1. Custom message when asking for Address Book Permissions http://kevinyavno.com/blog/?p=176
- 搭建Fabric网络(一)安装开发工具
Fabric V1.1.0已经发布了,这里准备一篇文章来介绍Fabric V1.1.0 网络怎么搭建. 安装cURL https://curl.haxx.se/download.html 安装Dock ...
- 20岁的设计师vs30岁的设计师
20岁的设计师vs30岁的设计师 如果你还是20来岁,要恭喜你,你还年轻, 一切才刚刚开始 还有时间去探索无尽的可能 还有时间去找到无限的前途 如果30岁的你还不够强大, 请记得时刻给予自己信心, ...
- 检查mysql是否运行
netstat -tunple|grep mysql
- Eclipse快速生成覆盖方法、Getter、Setter的方法
点击鼠标右键 --> Source --> 直接使用快捷键 Alt+Shift+s