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. Java线程状态图

    嘤,先盗图一张,后面再补充描述!

  2. Qt上FFTW組件的编译与安裝

    Qt上FFTW組件的編譯安裝 FFTW是一個做頻譜非常實用的組件,本文講述在Windows和Linux兩個平臺使用FFTW組件.Windows下的的FFTW組件已經編譯好成爲dll文件,按照開發應用的 ...

  3. MATLAB数学实验总结

    L1 MATLAB 基础知识 P6 表1-3 数据显示格式 format rat format long P20 表2-5 常用的矩阵函数 zeros(m,n) %零阵 eye(n) %单位阵 one ...

  4. 后端系统开发利器之gflags

    gflags是Google的一个开源项目,用于解析程序运行参数.gflags简单易用,它的好处在于统一配置格式,减少开发工作量.在工程实践中,gflags在简化开发和测试方面表现非常出色,它还有一个很 ...

  5. C/S与B/S 区别以及优缺点

    1.什么是C/S结构 C/S (Client/Server)结构,即客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现 ...

  6. jquery.validate验证,jquery.Form插件提交,主要可以异步提交文件

    <script type="text/javascript"> $(function () { $form = $("#manuForm"); $b ...

  7. 南京Uber优步司机奖励政策(7.20~7.26)

    人民优步奖励前提   *必须满足当周平均评分4.5星及以上,且当周接单率70%及以上,满足以上所有前提即可获得当周奖励 *刷单和红线行为立即封号并取消当周全部奖励及车费! 滴滴快车单单2.5倍,注册地 ...

  8. 宁波Uber优步司机奖励政策(1月4日~1月10日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. InnoDB意向锁和插入意向锁

      Preface       Last night one buddy in tech wechat group asked "what's intention locks of Inno ...

  10. Selenium(Python) ddt读取MySQL数据驱动

    import unittestfrom time import sleep from ddt import ddt, datafrom pymysql import connectfrom selen ...