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 ...
随机推荐
- CMake简易入门
使用CMake编译 CMake工具用于生成Makefile文件.用户通过编写CMakeLists.txt文件,描述构建过程(编译.连接.测试.打包),之后通过解析该文件,生成目标平台的Makefile ...
- 洛谷 P1379 八数码难题
题目描述 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了 ...
- scrollspy.js--bug
/** * 20140505 14.33 ycx * scrollspy.js中存在的bug!!!---为什么ui.tabs必须在scrollspy.js中的window.onload之前执行,也就是 ...
- 搭建DNS服务器-bind
1. 安装 yum install -y bind-chroot yum install -y bind-utils service named-chroot start 2. 修改配置 增加一 ...
- Hadoop问题集锦
1.Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup 使用Spark进行处理的时候 ...
- BZOJ1012:[JSOI2008]最大数
浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?id ...
- bzoj 2093 [Poi2010]Frog——滑动窗口
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2093 找第k近的可以用一个含k个元素的滑动窗口来实现. 卡空间也还行,但卡时间.不要预处理倍 ...
- C#中如何设置日期格式
在C#中,ToShortDateString()是用于显示短日期格式的方法,如果使用下面的语句: Label1.Text = DateTime.Now.ToShortDateString(); 那么, ...
- TP5隐藏url中的index.php
在public文件夹下,有个.htacess文件,没有则新建一个, 如果已有这个文件,原文件内容如下: <IfModule mod_rewrite.c> Options +FollowSy ...
- c语言基础 c和指针
句子 c规定数组名代表数组首元素的地址 如果&a 则代表整个数组 没有内存哪来的指针 数据类型的本质:固定大小内存的别名 变量的本质:(一段连续)内存空间的别名,内存空间的标号 指针是一种数据 ...