博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT1101:Quick Sort
阅读量:4322 次
发布时间:2019-06-06

本文共 3006 字,大约阅读时间需要 10 分钟。

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:
    51 3 2 4 5
    Sample Output:
    31 4 5

思路

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

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

1.从左至右遍历找不满足本身比左侧所有数大的点。

2.从右到左遍历找不满足本身比右侧所有数小的点(如果之前在从左到右的遍历中这个点已经不满足条件了就不用再计数了)。

3.打印结果就行

代码

 

#include
#include
using namespace std;vector
nums(100001);vector
check(100001,true);const int INITMAX = 1000000003;const int INITMIN = -233;using namespace std;int main(){ int N; while(cin >> N) { int countnum = N,lminnum = INITMIN,rmaxnum = INITMAX; for(int i = 0;i < N;i++) { cin >> nums[i]; if(nums[i] > lminnum) { lminnum = nums[i]; continue; } check[i] = false; countnum--; } for(int i = N - 1;i >=0;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 = 0;i < N;i++) { if(check[i]) { if(isFirst) { cout << nums[i]; isFirst = false; } else cout << " " << nums[i]; } } cout << endl; }}

 

 

 

转载于:https://www.cnblogs.com/0kk470/p/7689310.html

你可能感兴趣的文章
Node.SelectNodes
查看>>
Lambda表达式语法进一步巩固
查看>>
Vue基础安装(精华)
查看>>
Git 提交修改内容和查看被修改的内容
查看>>
PAT - 1008. 数组元素循环右移问题 (20)
查看>>
请求出现 Nginx 413 Request Entity Too Large错误的解决方法
查看>>
配置php_memcache访问网站的步骤
查看>>
hibernate的id生成策略
查看>>
树莓派3B+学习笔记:5、安装vim
查看>>
[Spfa][bfs] Jzoj P5781 秘密通道
查看>>
企业帐号进行IPA的打包、分发、下载安装的详细流程(转载)
查看>>
《项目架构那点儿事》——快速构建Junit用例
查看>>
{"errmsg":"invalid weapp pagepath hint: [IunP8a07243949]","errcode":40165}微信的坑
查看>>
DB2V9.5数据库使用pdf
查看>>
Java Bigdecimal使用
查看>>
SQL注入之绕过WAF和Filter
查看>>
jquery validate使用方法
查看>>
DataNode 工作机制
查看>>
windows系统下安装MySQL
查看>>
错误提示总结
查看>>