A1101. 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
#include<cstdio>
#include<iostream>
#include<limits.h>
using namespace std;
int num[], L[] = {,}, R[] = {,};
int main(){
int N, max = -, cnt = , index = -;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d", &num[i]);
}
for(int i = ; i < N; i++){
if(num[i] > max){
max = num[i];
L[i] = ;
}
}
int min = INT_MAX;
for(int i = N - ; i >= ; i--){
if(num[i] < min){
min = num[i];
R[i] = ;
}
}
for(int i = ; i < N; i++){
if(L[i] && R[i]){
cnt++;
}
if(L[i] && R[i] && index == -){
index = i;
}
}
if(cnt == )
printf("%d\n\n", cnt);
else
printf("%d\n%d", cnt, num[index]);
for(int i = index + ; i < N; i++){
if(L[i] && R[i])
printf(" %d", num[i]);
}
cin >> N;
return ;
}
总结:
1、与上一题一样的做法。注意无解的情况,应输出2个\n,以表示输出了2行, 否则会报格式错误。
2、注意min和max的初始值,输入的数字可能很大,最好让min和max在long long的范围足够小和足够大。
A1101. Quick Sort的更多相关文章
- PAT甲级——A1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- 【刷题-PAT】A1101 Quick Sort (25 分)
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- A1101 Quick Sort (25 分)
一.技术总结 这里的一个关键就是理解调换位置排序是时,如果是元主,那么它要确保的条件就只有两个一个是,自己的位置不变,还有就是前面的元素不能有比自己大的. 二.参考代码 #include<ios ...
- PAT_A1101#Quick Sort
Source: PAT A1101 Quick Sort (25 分) Description: There is a classical process named partition in the ...
- [算法]——快速排序(Quick Sort)
顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...
- quick sort 的简化实现
Pivot 随机选取意义不大 第一种方法使用随机pivot,使得尽可能平均二分序列,而实际上一般来说需要排序的集合往往是乱序的,无需重新生成随机数作为pivot,大可使用固定位置的数作为pivot,这 ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- [算法] 快速排序 Quick Sort
快速排序(Quick Sort)使用分治法策略. 它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分:其中一部分的所有数据都比另外一部分的所有数据都要小.然后,再按此方法对这 ...
- 基础排序算法之快速排序(Quick Sort)
快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...
随机推荐
- Jmeter(二十九)_dotnet搭建本地接口服务
这里使用的服务名为Bookshelf,在github上,自行下载.要运行此服务,需要.Net Core SDK 2.1或更高版本.如果尚未安装,从.Net Core官方网站下载并安装. 在本地克隆项目 ...
- zabbix监控-基本原理介绍
一.Linux下开源监控系统简单介绍1)cacti:存储数据能力强,报警性能差2)nagios:报警性能差,存储数据仅有简单的一段可以判断是否在合理范围内的数据长度,储存在内存中.比如,连续采样数据存 ...
- B. Divisor Subtraction
链接 [http://codeforces.com/contest/1076/problem/B] 题意 给你一个小于1e10的n,进行下面的运算,n==0 结束,否则n-最小质因子,问你进行多少步 ...
- 个人博客作业_week14
M1/M2阶段总结 我在M1阶段负责后端代码的开发,以及协助PM,在M2阶段负责PM,在为期将近一学期的团队软件开发过程中,我深刻体会到了团队协作的重要性,以及合理分配任务的重要性,没有一个好的时间规 ...
- Linux内核读书笔记第三周 调试
内核调试的难点在于它不能像用户态程序调试那样打断点,随时暂停查看各个变量的状态. 也不能像用户态程序那样崩溃后迅速的重启,恢复初始状态. 用户态程序和内核交互,用户态程序的各种状态,错误等可以由内核来 ...
- 第三个Sprint冲刺第九天(燃尽图)
- SprngMVC源码学习
运行helloWorld示例进入调试界面. DispatcherServlet:前端控制器 DispatcherServlet.doDispatch(HttpServletRequest, HttpS ...
- TortoiseSvn/Git的WaterEffect
https://github.com/TortoiseGit/TortoiseGit/blob/master/src/Utils/MiscUI/WaterEffect.cpp C#的实现: http: ...
- jmeter(psot) 表单提交 注意项
Form表单提交:直接使用表单方式提交即可,此方式采取页面直接跳转的形式.用jmeter请求的时候,日志打印传的参数是空的.考虑到form提交和http的提交请求头可能是不一样的,所以优化了请求头: ...
- From 简书 转帖一下如何安装k8s1.10 改天做下实验. https://www.jianshu.com/p/9c7e1c957752
centos7.3 kubernetes/k8s 1.10 离线安装 老菜_misa 关注 2018.04.25 23:57 字数 1243 阅读 266评论 1喜欢 3 本文介绍在centos7.3 ...