PAT 甲级 1101 Quick Sort
https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480
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
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int a[maxn], b[maxn];
vector<int> ans; int main() {
scanf("%d", &N);
for(int i = 0; i < N; i ++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b + N);
int maxx = INT_MIN;
for(int i = 0; i < N; i ++) {
if(a[i] == b[i] && a[i] > maxx)
ans.push_back(a[i]);
maxx = max(a[i], maxx);
}
if(ans.size()) {
printf("%d\n", ans.size());
for(int i = 0; i <ans.size(); i ++) {
if(i != 0) printf(" ");
printf("%d", ans[i]);
}
}
else printf("0\n");
printf("\n");
return 0;
}
就是看原数组有多少个数字的位置在排序后不变而且大于原数列中每一个在他前面的数字 没想到提交的时候出现了格式错误 当不存在的时候还是要多输出一个换行 不是指输出一个 $0$ 然后直接换行
PAT 甲级 1101 Quick Sort的更多相关文章
- PAT甲级——1101 Quick Sort (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分) ...
- 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 (25)
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 ...
- 1101 Quick Sort(25 分
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
随机推荐
- 解决thinkphp query()执行原生SQL语句成功结果报错的问题
1.query方法 query方法用于执行SQL查询操作,如果数据非法或者查询错误则返回false,否则返回查询结果数据集(同select方法). 2.execute方法 execute用于更新和写入 ...
- 帝国cms教程父栏目和子栏目都能在当前栏目高亮
首先在/e/class/userfun.php这个文件里面加上下面代码.上面父栏目的,下面子栏目的.红色代表css样式.自定义吧 function currentPage($classid,$this ...
- Oracle之多表查询
-多表查询 1.交叉连接 select * from t_class for update; select * from t_student for update; select for update ...
- core dump文件分析和调试
core介绍 当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内存状态记录下来,保存在一个文件中,这种行为就叫做Core Dump(中文有的翻译成"核心转储").我们可以认 ...
- Altium Designer常用快捷键
一:Altium原理图快捷键: Shift+左键选择 :实现多个目标选择 Ctrl+左键拖动 :保持连线拖动目标 Shift+c :清除当前过滤(?? ...
- 几个简单if程序的细节比较与加法程序设计
关于简单的if判断语句的不同写法: 输出0-9十个整数: 第一个程序: #include<stdio.h> #include<stdlib.h> int main() { ; ...
- mRNA翻译成蛋白
dna = "ATGCACGTGCGCTCACTGCGAGCTGCGGCGCCGCACAGCTTCGTGGCGCTCTGGGCACCCCTGTTCCTGCTGCGCTCCGCCCTGGCCG ...
- EEPROM读写学习笔记与I2C总线(二)
无论任何电子产品都会涉及到数据的产生与数据的保存,这个数据可能并不是用来长久保存,只是在运行程序才会用到,有些数据体量较大对于获取时效性并不太强,各种各样的数据也就有不同的存储载体,这次在EEPROM ...
- java 第七章 面向对象高级特性
一.类的继承 (一)继承的含义 1.在Java中定义一个类时,让该类通过关键字extends继承一个已有的类,这就是类的继承(泛化). 2.被继承的类称为父类(超类,基类),新的类称为子类(派生类). ...
- 20145234黄斐《信息安全系统设计基础》第八周(Linux下vim相关命令)
Linux下vim相关命令 在编辑程序时经常使用vim,所以记住一些常用的指令还是很有必要的 文件命令 vim file 打开单个文件vim file vim file1 file2 file3 .. ...