PAT甲级——1101 Quick Sort (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846
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
题目大意:找出数据中的主元并按从小到大顺序输出。在原始数据中,若一个数比它左边的所有的数都大并且比它右边所有的数都小,那它就是主元(pivot)。
思路:将原始数据a保存一个副本b,调用库函数sort将数据排序。遍历数组,用max记录到达当前位置时b数组中的最大值,若某个数字排序前后的位置没有发生改变即 a[i] = b[i] 且它 ≥ max,那么它就是pivot(主元)。(题目规定了没有相同的数字,而且都是正整数,所以该数字位置不变且大于左边的所有数字就可以保证它也小于右边的所有数字)
一开始我误解题意了,以为就是快排里面的主元,写了个快排发现没有卵用~ 其实此主元非彼主元,快排里的主元是人为设置的,题目要找的是原始数据里的主元。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; int main()
{
int N, max = -;
scanf("%d", &N);
vector <int> a, b, ans;
a.resize(N);
b.resize(N);
for (int i = ; i < N; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(a.begin(), a.end());
for (int i = ; i < N; i++) {
if (max < b[i]) {
max = b[i];
}
if (a[i] == b[i] && b[i] >= max) {
ans.push_back(a[i]);
}
}
int m = ans.size();
printf("%d\n", m);
for (int i = ; i < m; i++) {
printf("%d", ans[i]);
if (i < m - )
printf(" ");
}
printf("\n");//少了换行符会有一个测试点格式错误
return ;
}
PAT甲级——1101 Quick Sort (快速排序)的更多相关文章
- PAT 甲级 1101 Quick Sort
https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...
- PAT甲1101 Quick Sort
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- PAT甲级——A1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT 1101 Quick Sort[一般上]
1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...
- 【刷题-PAT】A1101 Quick Sort (25 分)
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- PAT 1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- 1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- Quick Sort(快速排序)
Quick Sort Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
随机推荐
- H.264 码率设置
一.什么是视频码率 视频码率是视频数据(包含视频色彩量.亮度量.像素量)每秒输出的位数.一般用的单位是kbps. 二.设置视频码率的必要性 在网络视频应用中,视频质量和网络带宽占用是相矛盾的.通常情况 ...
- bzoj2654tree
给定一个n个点m条边的图,每条边有黑白两色,求出恰好含need条白边的最小生成树 最小生成树...仿佛并没有什么dp的做法 大概还是个kruskal的板子再加点什么东西 考虑到“恰好含need条白边” ...
- 解决苹果手机Safari浏览器下 字体显示为 蓝色的 问题
解决苹果手机 Safari浏览器下 字体显示为蓝色的 问题 近期测试同学测试,wap站上,底部文字在苹果8上面 ,使用 Safari浏览器打开,一直显示 蓝色字体 其他正常,寻找半天无解,最后 阳 ...
- Oracle中spool命令实现的两种方法比较
---恢复内容开始--- 要输出符合要求格式的数据文件只需在select时用字符连接来规范格式.比如有如下表 SQL>; select id,username,password from myu ...
- python setuptools安装
执行# python setup.py install 时发生如下错误 Traceback (most recent call last): File "setup.py", li ...
- HDOJ(1069)最长下降子序列
每个箱子可有3种叠加方式,所以有3*n个箱子.将箱子按长度由大到小排序,有求箱子按宽度的最长下降子序列的高度之和即可. #include<cstdio> #include<algor ...
- web攻击之五:上传漏洞
[攻击] 在图片上传的时候,攻击者上传非图片,而是可远程执行的的脚本,这时候,入侵者就可以远程的执行脚本来对服务器进行攻击 [防御] 1.限制文件上传类型 2.使用第三方文件托管等
- 不卸载ceph重新获取一个干净的集群环境
不卸载ceph重新获取一个干净的集群环境 标签(空格分隔): ceph ceph环境搭建 运维 部署了一个ceph集群环境,由于种种原因需要回到最开始完全clean的状态,而又不想卸载ceph客户端或 ...
- DevExpress 柱状图
通过构造函数,把值传递过来 public XtraInterpreterChartForm(object ds) { InitializeComponent(); datasource = ds; } ...
- 文件格式——gff格式
Gff文件格式 gff格式是Sanger研究所定义,是一种简单的.方便的对于DNA.RNA以及蛋白质序列的特征进行描述的一种数据格式,已经成为序列注释的通用格式,比如基因组的基因预测,许多软件都支持输 ...