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的更多相关文章

  1. PAT甲级——1101 Quick Sort (快速排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分)   ...

  2. PAT甲1101 Quick Sort

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  3. PAT甲级——A1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  4. PAT 1101 Quick Sort[一般上]

    1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...

  5. 【刷题-PAT】A1101 Quick Sort (25 分)

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  6. PAT 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  7. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  8. 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  9. 1101 Quick Sort(25 分

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

随机推荐

  1. Python系列之入门篇——pytables及其客户端

    pytables及其客户端查看 pytables # ubuntu sudo apt-get install python-tables pip install flask flask-httpaut ...

  2. Linux内核调用I2C驱动_驱动嵌套驱动方法

    禁止转载!!!! Linux内核调用I2C驱动_以MPU6050为例 0. 导语 最近一段时间都在恶补数据结构和C++,加上导师的事情比较多,Linux内核驱动的学习进程总是被阻碍.不过,十一假期终于 ...

  3. Java学习笔记十七:Java中static使用方法

    Java中static使用方法 一:Java中的static使用之静态变量: 我们都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的 ...

  4. BZOJ1085_骑士精神_KEY

    题目传送门 乍一看好像是搜索题,但搜索明显会超时. 此处采用IDA*的方法求解. IDA*算法就是基于迭代加深的A*算法. code: /******************************* ...

  5. productFlavors 差异打包问题

    差异化打包: 1.dependencies compile 是不可以放到差异化的productFlavors里面的. 会报错: Error:(69, 0) Could not find method ...

  6. springboot jpa操作redis

    SpringBoot使用Redis缓存   (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boo ...

  7. Fiddler使用总结(一)

    Fiddler基础知识 .Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的,使用的代理地址是:127.0.0.1,端口默认为8888,我们也可以通过设置进行修改. .代理就 ...

  8. leetcode-分割回文子串

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

  9. 数据库Mysql的学习(五)-运算符与函数

    ,store,store,store,store FROM bookinfo;//加减乘除取余 //余额大于200 //余额不等于200 SELECT * FROM readerinfo WHERE ...

  10. 在使用Pipeline串联多个stage时model和非model的区别

    train.csv数据: id,name,age,sex1,lyy,20,F2,rdd,20,M3,nyc,18,M4,mzy,10,M 数据读取: SparkSession spark = Spar ...