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有多少个并且输出来. 大水题,正循环和倒着 ...
随机推荐
- JSTL判断list的size()大小,以及choose(相当于if else作用)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ tag ...
- windows下安装MySQL-python遇到的问题
执行安装命令 pip install MySQL-python 一.执行时会报一个错误 error: Microsoft Visual C++ 9.0 is required (Unable to f ...
- 62. ExtJS + fileuploadfield实现文件上传
转自:https://www.cnblogs.com/yzuzhang/p/5128174.html 后台服务端接收文件的代码: /** * 后台上传文件处理Action */ @RequestMap ...
- Linux进程状态解析
引言 Linux是一个多用户,多任务的系统,可以同时运行多个用户的多个程序,就必然会产生很多的进程,而每个进程会有不同的状态. 在下文将对进程的R.S.D.T.Z.X 六种状态做个说明. PROCE ...
- Linux扩展正则表达式
1. 扩展正则表达式 1.1 +(加号) + 表示前一个字符出现1次或1次以上 1.1.1 理解+ 要求:取出文件内容连续出现的小写字母 [root@oldboyedu50-lnb /oldboy]# ...
- 简单理解jsonp原理
对于javascript程序员来说,发送ajax请求获取后台数据然后把数据和模板拼接成字符串渲染回DOM实现无刷新更新页面这样的操作可谓是轻车熟路.但众所周知,ajax有一个不好,就是不能跨域传输数据 ...
- SCOI2014总结
似乎还没有写过SCOI的总结,今天补上,权当填坑. PS:CDQZ的看到了不要到处黑 SCOI-2014应该算是我的小高考,感觉拿住一本招的瓶颈就在这里.加之NOIp只有400分有点拖后腿,所以很早就 ...
- ACM_Mystery
Mystery Time Limit: 2000/1000ms (Java/Others) Problem Description: No Description Input: The first l ...
- 【转】Linux下history命令用法
转自:http://blog.sina.com.cn/s/blog_5caa94a00100gyls.html 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你 ...
- Js控制样式的诸多方法
function TableCss(options){ //如果没参数,就退出 if(arguments.length < 1 || !document.getElementById(optio ...