1101. Quick Sort (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

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

思路

实现快排中取划分点的这一部分代码(而且只是一种最简单的找划分点的方法)。

题目要求让你统计一组数中有哪些数满足划分点的要求。

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的更多相关文章

  1. pat1101. Quick Sort (25)

    1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...

  2. [算法]——快速排序(Quick Sort)

    顾名思义,快速排序(quick sort)速度十分快,时间复杂度为O(nlogn).虽然从此角度讲,也有很多排序算法如归并排序.堆排序甚至希尔排序等,都能达到如此快速,但是快速排序使用更加广泛,以至于 ...

  3. quick sort 的简化实现

    Pivot 随机选取意义不大 第一种方法使用随机pivot,使得尽可能平均二分序列,而实际上一般来说需要排序的集合往往是乱序的,无需重新生成随机数作为pivot,大可使用固定位置的数作为pivot,这 ...

  4. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  5. [算法] 快速排序 Quick Sort

    快速排序(Quick Sort)使用分治法策略. 它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分:其中一部分的所有数据都比另外一部分的所有数据都要小.然后,再按此方法对这 ...

  6. 基础排序算法之快速排序(Quick Sort)

    快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...

  7. 快速排序(Quick Sort)的C语言实现

    快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤 ...

  8. Quick Sort In-place Implementation

    在线运行PHP http://www.compileonline.com/execute_php_online.php <?php function swap( &$a, &$b ...

  9. 快速排序(Quick Sort)

    快速排序是初学者比较难理解的几个算法之一,这里尽可简单化地讲解,希望能帮到大家. 快速排序基本步骤: 从数列中挑出一个元素,称为"基准"(pivot). 重新排序数列,所有元素比基 ...

随机推荐

  1. 【Qt编程】Qt学习笔记<三>

    1.      如果程序中使用了png以外格式的图片,在发布程序时就要将Qt安装目录下plugins中的imagineformats文件复制到发布文件中. 2.      在函数声明处快速添加函数定义 ...

  2. Graph Cut and Its Application in Computer Vision

    Graph Cut and Its Application in Computer Vision 原文出处: http://lincccc.blogspot.tw/2011/04/graph-cut- ...

  3. 【一天一道LeetCode】#4 Median of Two Sorted Arrays

    一天一道LeetCode (一)题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find th ...

  4. Gradle 1.12用户指南翻译——第三十九章. IDEA 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  5. IP网际协议 - IP首部,IP路由选择,子网掩码

    IP首部 4个字节的32 bit值以下面的次序传输:首先是0-7 bit,其次8-15 bit,然后1 6-23 bit,最后是24~31 bit.这种传输次序称作big endian字节序.由于T ...

  6. TCP连接建立系列 — 服务端发送SYNACK段

    本文主要分析:服务器端如何构造和发送SYNACK段. 内核版本:3.6 Author:zhangskd @ csdn blog 发送入口 tcp_v4_send_synack()用于发送SYNACK段 ...

  7. C# DataTable,DataSet,IList,IEnumerable 互转扩展属性

    using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R ...

  8. PS 滤镜——素描算法(一)

    这个算法结合高斯滤波和图层混合中的颜色减淡模式实现. 可以参考相关博客: http://blog.csdn.net/wsfdl/article/details/7610634 本文增加了一点调色,使得 ...

  9. zookeeper 事务日志

    前面提到,在zookeeper server的配置文件zoo.cfg中可以通过dataLogDir来配置zookeeper的事务日志的输出目录,这个事务日志类似于下面这样的文件: 这个文件是一个二进制 ...

  10. nasm中的表达式

    nasm表达式支持2个特殊的记号 $和$$;前者标识其所在源码行的开始处地址,所以你可以这样写死循环: jmp $ 而后者标识当前段开始处的地址,你可以通过: $-$$ 找出当前代码在段内的偏移. n ...