1101 Quick Sort (25 分)
 

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;
#define N 100010
int a[N],n;
int vis[N];
vector<int>ve;
//有多少个数它左边比它小,右边比它大
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i =;i<=n;i++) vis[i] = ;
int maxx = a[];
//因为是N个不同的数,那么从左到右应该是递增
//只要不满足这个条件,就一定不是,逆序时同理
for(int i =;i<=n;i++){
if(a[i]<maxx) vis[i] = ;
else{
maxx = a[i];
}
}
int minn =a[n];
for(int i=n;i>=;i-- ){
if(a[i]>minn) vis[i] = ;
else {
minn =a[i];
}
}
int cnt = ;
for(int i =;i<=n;i++){
if(vis[i]==)
{
cnt++;
ve.push_back(a[i]) ;
}
}
if(cnt==) printf("0\n\n");
else{
printf("%d\n",cnt);
sort(ve.begin(),ve.end());
for(int i =;i<ve.size();i++){
if(i==) printf("%d",ve[i]);
else{
printf(" %d",ve[i]);
}
}
}
return ;
}
 

快排 PAT 1101的更多相关文章

  1. F#之旅4 - 小实践之快排

    参考文章:https://swlaschin.gitbooks.io/fsharpforfunandprofit/content/posts/fvsc-quicksort.html F#之旅4 - 小 ...

  2. 快排 快速排序 qsort quicksort C语言

    现在网上搜到的快排和我以前打的不太一样,感觉有点复杂,我用的快排是FreePascal里/demo/text/qsort.pp的风格,感觉特别简洁. #include<stdio.h> # ...

  3. iOS常见算法(二分法 冒泡 选择 快排)

    二分法: 平均时间复杂度:O(log2n) int halfFuntion(int a[], int length, int number)  { int start = 0; int end = l ...

  4. C++ 快排

    // 进行一轮快排并返回当前的中间数 int getMiddle( int* arr, int low, int high ) { auto swaparr = [&]( int i, int ...

  5. 先贴上代码:Random快排,快排的非递归实现

    设要排序的数组是A[0]……A[N-1],首先任意选取一个数据(通常选用数组的第一个数)作为主元,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序.值得注意的是, ...

  6. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  7. ACM/ICPC 之 快排+归并排序-记录顺序对(TSH OJ-LightHouse(灯塔))

    TsingHua OJ 上不能使用<algorithm>头文件,因此需要手写快排(刚开始写的时候自己就出了很多问题....),另外本题需要在给横坐标排序后,需要记录纵坐标的顺序对的数量,因 ...

  8. 数组第K小数问题 及其对于 快排和堆排 的相关优化比较

    题目描述 给定一个整数数组a[0,...,n-1],求数组中第k小数 输入描述 首先输入数组长度n和k,其中1<=n<=5000, 1<=k<=n 然后输出n个整形元素,每个数 ...

  9. 结构体快排回顾(sort)

    一般来说,我做竞赛的时候排序一般用快排 很快很方便 普通sort(从小到大) sort(a,a+n); 直接贴一段代码吧,包含了vector,sort,结构体等简单东西综合 #include < ...

随机推荐

  1. DSL的本质:领域构建的半成品

    DSL的本质是使用通用和专用语言构建领域的半成品: 实际上是构建了一个世界观.小宇宙的半成品: 这个半成品包含领域的基本要素.联系方式和基本运行规律: 开发者使用这个半成品平台进行开发能达到事半功倍. ...

  2. linux查找与替换练习

    查找和替换-举例 删除/tmp/abc文件中第 2 至 5 行的内容 在第 2 行后面添加 123456 这一行 在文件的最后一行前面添加 123456 将文件中的 cat全部替换成 dog 注以上操 ...

  3. vue项目开发期间,配置webpack解决后台接口在不同服务器上的问题 之 二 ( node搭建服务 )

    由于今天上午 后端人员把接口都整合都一个服务器了,所以就没有硬关注 上一篇文章的问题, 晚上回来,用node搭了一个简单服务器,测试了下,是没有问题的.代码如下: 一. 自己初始化项目, 1.pack ...

  4. Numpy | 07 从数值范围创建数组

    numpy.arange ***** 使用numpy 包中的 arange 函数,创建数值范围并返回 ndarray 对象,函数格式如下: numpy.arange(start, stop, step ...

  5. 一起学Makefile(六)

    命令的回显: 通常,make在执行命令之前都会把执行的命令进行输出,例如: 关闭命令回显有以下几种方式: 每个需要关闭回显的命令行之前加上”@”符号: 执行make时机上参数-s 或 –slient进 ...

  6. [算法模板]ST表

    [算法模板]ST表 ST表和线段树一样,都能解决RMQ问题(范围最值查询-Range Minimum Query). 我们开一个数组数组\(f[maxn][maxn\log_2]\)来储存数据. 定义 ...

  7. Centos7 U盘安装&命令大全

    软件下载 1.centos下载,下载地址https://www.centos.org/download/ 我选择的镜像是:CentOS-7-x86_64-DVD-1804.iso 2.UltraISO ...

  8. Redis简介及其安装

    1.Redis NoSQL (Not noly SQL)不仅仅是SQL 属于非关系型数据库:Redis就属于非关系型数据库 传统的Mysql ,oracle ,sql server 等 都是关系型数据 ...

  9. Element + Vue I18n动态import加载国际化语言包翻译文件

    需求 项目为多页应用,包含产品a.b.c.d.e,每个产品都有自己的翻译文件.一次加载所有翻译文件是极度不合理的.于是考虑动态加载. 实现 参考官方文档:延迟加载翻译 项目结构 │ ├── dist ...

  10. Unity2D音游案例-节奏大师教程+源码+素材

    Unity2D音游案例-节奏大师(Unity2017.2 ,基于Koreographer),本案例涉及到一些音乐音频音波的一点点内容. 首先,我们会把音游的核心功能实现,之后几个重要内容会出给思路并带 ...