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. day 29 socketserver ftp功能的简单讲解

    1.上传下载的简单示例 server: import socket import struct import json   server =socket.socket() server.bind((' ...

  2. 计算机基础和Linux基础

    计算机原理 计算机发展史 机器语言—让机器干活 差分机—让机器的数学运算和逻辑运算只简化成“加法”,计算机只处理“加法” 计算机硬件CPU=运算器+控制器+寄存器(缓存)硬盘=存储器+寄存器寄存器是为 ...

  3. CRM2Stark组件

    CRM stark组件 开启新项目:CRM 再创建一个应用app02(python manage.py startapp app02 或者是工具栏:run...task:执行startapp app0 ...

  4. golang实现LRU,转载学习

    package main type LRUNode struct { key string val interface{} prev *LRUNode next *LRUNode } type LRU ...

  5. 数据结构之栈和队列及其Java实现

    栈和队列是数据结构中非常常见和基础的线性表,在某些场合栈和队列使用很多,因此本篇主要介绍栈和队列,并用Java实现基本的栈和队列,同时用栈和队列相互实现. 栈:栈是一种基于“后进先出”策略的线性表.在 ...

  6. mySql——case when else ....demo

    DROP PROCEDURE IF EXISTS Pro_query_change_charge_by_layer_report; CREATE PROCEDURE Pro_query_change_ ...

  7. vijos p1027休息中的小呆

    休息中的小呆 描述 当大家在考场中接受考验(折磨?)的时候,小呆正在悠闲(欠扁)地玩一个叫“最初梦想”的游戏.游戏描述的是一个叫pass的有志少年在不同的时空穿越对抗传说中的大魔王chineseson ...

  8. Java:break和continue关键字的作用

    二者的作用和区别 1. break:直接跳出当前循环体(while.for.do while)或程序块(switch).其中switch case执行时,一定会先进行匹配,匹配成功返回当前case的值 ...

  9. mongoDB在java上面的应用

    1.实际应用过程中肯定不会直接通过Linux的方式来连接和使用数据库,而是通过其他驱动的方式来使用mongoDB 2.本教程只针对于Java来做操作,主要是模拟mongoDB数据库在开发过程中的应用 ...

  10. elasticsearch增删改查操作

    目录 1. 插入数据 2. 更改数据 3. 删除数据 4. 检索文档 1. 插入数据 关于下面的代码如何使用,可以借助于kibana的console,浏览器打开地址: http://xxx.xxx.x ...