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有多少个并且输出来. 大水题,正循环和倒着 ...
随机推荐
- bootstrap 折叠collapse失效
手动点击折叠,然后调用折叠全部以后,在手动点击折叠项,折叠失效. 方法,折叠项是通过添加或删除".in"来实现,实现如下 $(".collapse.in").c ...
- JFinal Web开发学习(八)后台集成H-ui-admin前端框架
h-ui-admin是一个很不错的前端框架h-ui实现的一个后台管理系统的前端. 1.在WebRoot目录下新建admin目录 2.下载h-ui-admin(当前最新是2.5版本)并解压至admin文 ...
- 梦殇 chapter five
一弦情殇未谱,半纸离愁难书,不记年,叹花开几度...... 蜡烛用它自己的死亡来温暖别人,奉献自己的同时,它内心是快乐的吗?那残留的蜡油是它的泪水,还是它快乐的预兆? 这个世界上除了父母家人外,会有真 ...
- 在threejs中对3D物体旋转的思考
今天在写threejs时,突然想到一个问题:一个3D物体需要旋转时,一般情况下简单的旋转我都是使用欧拉角,稍微复杂一些的情况我会把欧拉角转换成四元数进行旋转(欧拉角复杂旋转可能会产生的死锁问题),但是 ...
- 对于devexpress gridview 内插图加加进度条等的一点解读
如上图,gategory 加了小图标, 其他行内还有计算器,大图片 进度条等 using System; using System.Drawing; using System.Collection ...
- Mac Terminal
一.简介 二.实用 1)update-apps-using-terminal-mac https://www.maketecheasier.com/update-apps-using-termin ...
- springboot 项目添加jaeger调用链监控
1.添加maven依赖<dependency> <groupId>io.opentracing.contrib</groupId> <artifactId&g ...
- prometheus 通过企业微信接收告警
准备工作 step 1: 访问网站 注册企业微信账号(不需要企业认证). step 2: 访问apps 创建第三方应用,点击创建应用按钮 -> 填写应用信息: prometheus 配置: # ...
- 简单DP入门四连发
复习一下一直不太懂的dp. dp博大精深,路还长着呢 第一题;http://acm.hdu.edu.cn/showproblem.php?pid=2084 从下往上就是水题 #include<c ...
- 冒泡排序(js版)
基本思想:两两比较相邻记录的关键字,如果反序则交换,直至没有反序为止. 最初的冒泡排序(初级版): //从小到大 function BubbleSort(arr){ var i,j,temp; for ...