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有多少个并且输出来. 大水题,正循环和倒着 ...
随机推荐
- WebSocket握手总结
网址:http://blog.csdn.net/edwingu/article/details/44040961 WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器 ...
- 写web项目注意事项
1.中文名2.文件存放路径(js css img)3.class详细路径(mydiv.myul li)
- yum -y --downloadonly --downloaddir=/ruiy upggrde;
事务概要================================================================================================ ...
- Flink之Window Operation
目录 Configuring Time Characteristics Process Functions Window Operators Applying Functions on Windows ...
- Vue.js经典开源项目汇总-前端参考资源
Vue.js经典开源项目汇总 原文链接:http://www.cnblogs.com/huyong/p/6517949.html Vue是什么? Vue.js(读音 /vjuː/, 类似于 view) ...
- Vue中nextTick()解析
最近,在开发的时候遇到一个问题,让我对vue中nextTick()的用法加深了了解- 下面是在组件中引用的一个拖拽的组件: <vue-draggable-resizable class=&quo ...
- matlab中增加Java VM 的堆空间(解决xml_io_tools出现的OutOfMemory问题)
今天用MATLAB写程序,调用了xml_io_tools(很赞的一个xml读写工具包)中的函数,但是由于我要书写的文件比较大,5m左右,运行时不知道xml_io_tools中的哪一块超出了java中的 ...
- BZOJ 4304 tarjan+topsort+bitset
我就是想骗一骗访问量 先Tarjan搞出来所有的强连通分量 正向连边 反向连边 topsort一发 搞出来每个点可以到哪些点 和哪些点可以到这个点 对于每条边 与一下 就是答案 //By Siri ...
- Windows10开启热点
1.以网线的连接方式,已经连接. 2.打开CMD 3. 开启热点 3.1设置热点名称和密码 netsh wlan set hostednetwork mode=allow ssid=name key= ...
- TCP的send与recv函数小结
Send函数: 在阻塞模式下, send函数的过程是将应用程序请求发送的数据拷贝到发送缓存中发送并得到确认后再返回.但由于发送缓存的存在,表现为:如果发送缓存大小比请求发送的大小要大,那么send函数 ...