博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——1101 Quick Sort (快速排序)
阅读量:5264 次
发布时间:2019-06-14

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

本文同步发布在CSDN:

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 (≤). Then the next line contains N distinct positive integers no larger than 1. 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

题目大意:找出数据中的主元并按从小到大顺序输出。在原始数据中,若一个数比它左边的所有的数都大并且比它右边所有的数都小,那它就是主元(pivot)。

思路:将原始数据a保存一个副本b,调用库函数sort将数据排序。遍历数组,用max记录到达当前位置时b数组中的最大值,若某个数字排序前后的位置没有发生改变即 a[i] = b[i] 且它 ≥ max,那么它就是pivot(主元)。(题目规定了没有相同的数字,而且都是正整数,所以该数字位置不变且大于左边的所有数字就可以保证它也小于右边的所有数字)

一开始我误解题意了,以为就是快排里面的主元,写了个快排发现没有卵用~   其实此主元非彼主元,快排里的主元是人为设置的,题目要找的是原始数据里的主元。

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 int main() 7 { 8 int N, max = -1; 9 scanf("%d", &N);10 vector
a, b, ans;11 a.resize(N);12 b.resize(N);13 for (int i = 0; i < N; i++) {14 scanf("%d", &a[i]);15 b[i] = a[i];16 }17 sort(a.begin(), a.end());18 for (int i = 0; i < N; i++) {19 if (max < b[i]) {20 max = b[i];21 }22 if (a[i] == b[i] && b[i] >= max) {23 ans.push_back(a[i]);24 }25 }26 int m = ans.size();27 printf("%d\n", m);28 for (int i = 0; i < m; i++) {29 printf("%d", ans[i]);30 if (i < m - 1)31 printf(" ");32 }33 printf("\n");//少了换行符会有一个测试点格式错误34 return 0;35 }

 

转载于:https://www.cnblogs.com/yinhao-ing/p/10934205.html

你可能感兴趣的文章
语音识别中的MFCC的提取原理和MATLAB实现
查看>>
验证组件FluentValidation的使用示例
查看>>
0320-学习进度条
查看>>
解决windows系统的oracle数据库不能启动ora-00119和ora-00130的问题
查看>>
ip相关问题解答
查看>>
MetaWeblog API Test
查看>>
反弹SHELL
查看>>
关闭Chrome浏览器的自动更新和升级提示
查看>>
移动、尺寸改变
查看>>
poj2255Tree Recovery【二叉树重构】
查看>>
tcpcopy 流量复制工具
查看>>
vue和react的区别
查看>>
第十一次作业
查看>>
负载均衡策略
查看>>
微信智能开放平台
查看>>
ArcGIS Engine 中的绘制与编辑
查看>>
Oracle--通配符、Escape转义字符、模糊查询语句
查看>>
c# 文件笔记
查看>>
第一页 - 工具的使用(webstorm)
查看>>
Linux 进程资源用量监控和按用户设置进程限制
查看>>