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