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 (<= 105). Then the next line contains N distinct positive integers no larger than 109. 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

分析

这道题就是求一列数中比左边所有数大,比右边所有数小的数是哪些,其实只需要创建两个vector MAX, MIN.

MAX[i]表示vec[i]左边的所有数中最大的数,

MIN[i]表示vec[i]右边的所有数中最小的数。

其中初始化MAX[0]=0,MIN[N-1]=1000000001;

MAX[i]=max(MAX[i-1], vec[i-1]);

MIN[i]=min(MIN[i+1], vec[i+1]);

如果vec[i]比MAX[i]大,比MIN[i]小,就满足条件。

特别提醒最后还要输出换行,不然有个测试点过不了

#include<iostream> //最后要输出换行符
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
int main(){
int N;
cin>>N;
vector<int> vec(N,0);
for(int i=0; i<N; i++)
cin>>vec[i];
vector<int> Max(N,0), Min(N,0);
Max[0]=0; Min[N-1]=1000000001;
for(int i=1; i<N; i++)
Max[i]=max(Max[i-1], vec[i-1]);
for(int i=N-2; i>=0; i--)
Min[i]=min(Min[i+1], vec[i+1]);
vector<int> ans;
for(int i=0; i<N; i++)
if(vec[i]>Max[i]&&vec[i]<Min[i])
ans.push_back(vec[i]);
sort(ans.begin(), ans.end());
cout<<ans.size()<<endl;
for(int i=0; i<ans.size(); i++)
i==0?cout<<ans[i]:cout<<" "<<ans[i];
cout<<endl;
return 0;
}

PAT 1101 Quick Sort的更多相关文章

  1. PAT 1101 Quick Sort[一般上]

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

  2. PAT甲1101 Quick Sort

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

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

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

  4. PAT 甲级 1101 Quick Sort

    https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...

  5. 1101. Quick Sort (25)

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

  6. 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. PAT (Advanced Level) 1101. Quick Sort (25)

    树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...

  9. PAT甲题题解-1101. Quick Sort (25)-大水题

    快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...

随机推荐

  1. ping测试局域网内主机是否alive

    [root@zabbix ~]# cat alivehost.sh #!/bin/bash #Checks to see if hosts 192.168.1.100-192.168.1.200 ar ...

  2. git的基本操作流程

    1.git clone 初始会有默认的master分支,并且master和origin/master自动建立了映射关系 2. git checkout -b local    创建并且切换到local ...

  3. Mysql的简单使用(一)

    如果你会查询这些相关的问题,说明你是一个正在或者准备从事IT的程序猿,对于一个程序猿而言,不会使用linux系统的程序猿不是一好的程序猿哦!因为windows有时候真的让人很抓狂,而本人也相信没有什么 ...

  4. bzoj 1078 [SCOI2008]斜堆 —— 斜堆

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1078 考察斜堆的性质: 一个点如果没有左子树,也一定没有右子树: 看了这篇精美的博客:htt ...

  5. stm32I/O的8种工作模式

    转载自原子的论坛: 浮空,顾名思义就是浮在空中,上面用绳子一拉就上去了,下面用绳子一拉就沉下去了. 开漏,就等于输出口接了个NPN三极管,并且只接了e,b. c极 是开路的,你可以接一个电阻到3.3V ...

  6. sql数据库CHECKDB时报x个分配错误和x个一致性错误

    --1.在SQL查询分析器中执行以下语句:(注以下所用的POS为数据库名称,请用户手工改为自己的数据库名) use pos dbcc checkdb --2.查看查询结果,有很多红色字体显示,最后结果 ...

  7. 使用psutil模块获取电脑运行信息

    psutil是python的一个用于获取cpu信息的模块,非常好使,以下附上官方的一些example: CPU-> Examples >>> import psutil > ...

  8. SpringBoot入门之HelloWorld

    1.SpringBoot简介 百度百科:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而 ...

  9. 零基础如何学习Java和web前端

    今天说一下零基础到底能不能学习Java,为什么有的人说学不了呢,那么接下来我为大家揭晓,零基础到底适合不适合学习Java. 零基础学习Java的途径第一个就是看视频,然后就是看书,或者在线下报个培训班 ...

  10. JVM面试总结

    1.  Java虚拟机的内存布局(运行时数据区) 参考:https://www.cnblogs.com/lostyears/articles/8984171.html 2. GC算法及几种垃圾收集器 ...