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;
}
}

最新文章

  1. 第二章 spring
  2. 弹出框二 之 bootbox.js
  3. Jquery-UI实现弹出框样式
  4. netfilter分析
  5. MVC中html转义问题(直接输出html的方法)
  6. 【crunch bang】增加壁纸图片文件
  7. yabeblog.me 关于Tomcat7部署 一台机器部署两个项目,一个用域名访问,一个用IP访问
  8. 前端MVVM学习之KnockOut(二)
  9. AlarmReceiver 与IntentService的使用
  10. windows phone 页面导航(6)
  11. Mysql re-set password, mysql set encode utf8 mysql重置密码,mysql设置存储编码格式
  12. 即时作图新工具—ProcessOn【推荐】…
  13. CentOS 7安装Python3.5,并与Python2.7兼容并存
  14. [ExtJS5学习笔记]第二十节 Extjs5配合数组的push方法,动态创建并加载组件
  15. MySQL 基础知识梳理学习(五)----半同步复制
  16. SQL CE 和 SQLite数据库对比测试
  17. 周末班:Python基础之面向对象进阶
  18. [第二届构建之法论坛] 预培训文档(Java版)
  19. H5新增属性contenteditable(不用JS,实现div模拟textarea高度自增长)
  20. db mysql / mysql cluster 5.7.19 / my.cnf / max_binlog_cache_size / binlog

热门文章

  1. Hibernate统计表中的条数
  2. AndFix使用感想
  3. AngularJS进阶(九)控制器controller之间如何通信
  4. C/C++中如何产生伪随机数
  5. Detours修改段属性漏洞
  6. MarioTCP:一个单机可日30亿的百万并发长连接服务器
  7. 新闻网站开发-手机端-基于Wordpress
  8. PuTTY的下载安装和基本使用方法教程
  9. hadoop_eclipse及HDT插件的使用
  10. win10 下安装mysql服务器社区版本mysql-5.7.22-winx64