PAT 1101 Quick Sort
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的更多相关文章
- PAT 1101 Quick Sort[一般上]
1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...
- PAT甲1101 Quick Sort
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- PAT甲级——1101 Quick Sort (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分) ...
- PAT 甲级 1101 Quick Sort
https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- 1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- 1101 Quick Sort(25 分
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT (Advanced Level) 1101. Quick Sort (25)
树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...
- PAT甲题题解-1101. Quick Sort (25)-大水题
快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...
随机推荐
- QMap QHash的选择(QString这种复杂的比较,哈希算法比map快很多)
QMap QHash有近乎相同的功能.很多资料里面介绍过他们之间的区别了.但是都没有说明在使用中如何选择他们. 实际上他们除了存储顺序的差别外,只有key操作的区别. 哈希算法是将包含较多信息的“ke ...
- SPOJ - QMAX3VN (4350) splay
SPOJ - QMAX3VN 一个动态的序列 ,在线询问某个区间的最大值.关于静态序列的区间最值问题,用ST表解决,参考POJ 3264 乍一看上去 splay可以轻松解决.书上说可以用块状链表解决, ...
- LVS上DR和NAT模式的缺陷
引言 相信一般的小公司用的最多的还是DR和NAT模式,关于DR和NAT模式的原理请看看下图,我们先从lvs的DR和NAT模式特性聊聊一些问题. 问题1.lvs的DR模式和NAT模式核心缺陷有哪些? D ...
- 马拉车算法(Manacher's Algorithm)
这是悦乐书的第343次更新,第367篇原创 Manacher's Algorithm,中文名叫马拉车算法,是一位名叫Manacher的人在1975年提出的一种算法,解决的问题是求最长回文子串,神奇之处 ...
- ACM_小游戏(棋盘博弈)
Problem Description: 最近kiki无事可做,于是他想玩棋盘游戏.棋盘的大小是n * m.首先,棋子放置在右上角(1,m). 每次可以将棋子向左方,下方或左下方移动一个位置.当移动到 ...
- springboot自定义常量配置
现在你建一个类: import org.springframework.boot.context.properties.ConfigurationProperties; /** * Created b ...
- 实现微信小程序的wxml文件和wxss文件在phpstrom的支持
最近下载了微信小程序准备好好看看,但是发现微信小程序用的后缀名是不一样的,.wxml代表的就是平时用的.html,.wxss代码的就是平时用的.css.但是phpstorm无法识别,为了更方便的码代码 ...
- Android之Glide获取图片Path和Glide获取图片Bitmap
今天主要研究了Glide获取图片Path.Bitmap用法,相信也困扰了大家很久,我在网上也找了很久,基本没有,后来研究了下,也参考了下api文档,总结了以下几个方式: 1. 获取Bitmap: 1) ...
- Serializable和Parcelable的简单介绍
Serializable和Pacelable接口可以完成对象的序列化过程,当我们需要通过Intent和Binder传输数据时就需要使用Parcelable或者Serializable. Seriali ...
- java攻城狮之路--复习JDBC(数据库连接池 : C3P0、DBCP)
复习数据库连接池 : C3P0.DBCP 1.数据库连接池技术的优点: •资源重用: 由于数据库连接得以重用,避免了频繁创建,释放连接引起的大量性能开销.在减少系统消耗的基础上,另一方面也增 ...