PAT1101:Quick Sort
1101. Quick Sort (25)
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 5Sample Output:
3
1 4 5
思路
实现快排中取划分点的这一部分代码(而且只是一种最简单的找划分点的方法)。
题目要求让你统计一组数中有哪些数满足划分点的要求。
1.从左至右遍历找不满足本身比左侧所有数大的点。
2.从右到左遍历找不满足本身比右侧所有数小的点(如果之前在从左到右的遍历中这个点已经不满足条件了就不用再计数了)。
3.打印结果就行
代码
#include<iostream>
#include<vector>
using namespace std;
vector<int> nums();
vector<bool> check(,true);
const int INITMAX = ;
const int INITMIN = -;
using namespace std;
int main()
{
int N;
while(cin >> N)
{
int countnum = N,lminnum = INITMIN,rmaxnum = INITMAX;
for(int i = ;i < N;i++)
{
cin >> nums[i];
if(nums[i] > lminnum)
{
lminnum = nums[i];
continue;
}
check[i] = false;
countnum--;
}
for(int i = N - ;i >=;i--)
{
if(nums[i] < rmaxnum)
{
rmaxnum = nums[i];
continue;
}
if(check[i]) //避免重复计数
{
check[i] = false;
countnum--;
} }
bool isFirst = true;
cout << countnum << endl;
for(int i = ;i < N;i++)
{
if(check[i])
{
if(isFirst)
{
cout << nums[i];
isFirst = false;
}
else
cout << " " << nums[i];
}
}
cout << endl;
}
}
PAT1101:Quick Sort的更多相关文章
- pat1101. Quick Sort (25)
1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...
- [算法]——快速排序(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),它也是一个随机算法.最重要的是,快速排 ...
- 快速排序(Quick Sort)的C语言实现
快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤 ...
- Quick Sort In-place Implementation
在线运行PHP http://www.compileonline.com/execute_php_online.php <?php function swap( &$a, &$b ...
- 快速排序(Quick Sort)
快速排序是初学者比较难理解的几个算法之一,这里尽可简单化地讲解,希望能帮到大家. 快速排序基本步骤: 从数列中挑出一个元素,称为"基准"(pivot). 重新排序数列,所有元素比基 ...
随机推荐
- Robust Locally Weighted Regression 鲁棒局部加权回归 -R实现
鲁棒局部加权回归 [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. 算法参考文献: (1) Robust L ...
- 【Java编程】Java学习笔记<二>
种访问权限,而类的访问控制级别只有public和缺省的,当为public时,可以被任何包的其他类访问,当为缺省时,只能被同一包的其他类访问.如果类自身对另一个类是不可见的,即使类的成员声明为publi ...
- LeetCode之“数学”:Happy Number
题目链接 题目要求: Write an algorithm to determine if a number is "happy". A happy number is a num ...
- Oracle ERP系統借贷关系表
系统分步骤产生的分录: 1)库存模块作接收时产生的分录为: 借:材料采购 (采购单价X订单数量) 贷:应计负债 (采购单价X订单数量) 2)库存模块作检验入库时产生的分录为: 系统产生的分录分别为: ...
- studio grandle渠道打包
1. Mainfest 文件中添加一个键值对,这里的value 我定义为 "UMENG_CHANNEL_VALUE"(当然实际应用中可以根据自己的需要命名),后面打包的时候会对这 ...
- 如何使用ZOL一键安装器下载中关村在线的源安装包
如何使用ZOL一键安装器下载中关村在线的源安装包 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要 中关村在线最近开始推出ZOL一键安装器,用户直 ...
- Oracel 编写控制结构
1.条件分支语句 在Oracle9i之前,执行条件分支操作都需要使用IF语句来完成,并且PL/SQL中,提供了三种条件分支语句:IF-THEN.IF-THEN-ELSE.IF-THEN-ELSIF.具 ...
- sql语句——根据身份证号判断男女
根据身份证判断男女的规则:二代身份证为18位,判断倒数第二位,第二位若为奇数,性别为男:偶数则为女 一代身份证为15为,判断倒数第一位,规则同上. update 表名 set 表名.字段名= case ...
- SQL中内连接和外连接的问题!
本文系转载,版权归原作者所有. 如表 ------------------------------------------------- table1 | table2 | ...
- JMM
1.JMM简介 i.内存模型概述 Java平台自动集成了线程以及多处理器技术,这种集成程度比Java以前诞生的计算机语言要厉害很多,该语言针对多种异构平台的平台独立性而使用的多线程技术支持也是具有开拓 ...