本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846

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:

5
1 3 2 4 5

Sample Output:

3
1 4 5

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

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

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

 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std; int main()
{
int N, max = -;
scanf("%d", &N);
vector <int> a, b, ans;
a.resize(N);
b.resize(N);
for (int i = ; i < N; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(a.begin(), a.end());
for (int i = ; i < N; i++) {
if (max < b[i]) {
max = b[i];
}
if (a[i] == b[i] && b[i] >= max) {
ans.push_back(a[i]);
}
}
int m = ans.size();
printf("%d\n", m);
for (int i = ; i < m; i++) {
printf("%d", ans[i]);
if (i < m - )
printf(" ");
}
printf("\n");//少了换行符会有一个测试点格式错误
return ;
}

PAT甲级——1101 Quick Sort (快速排序)的更多相关文章

  1. PAT 甲级 1101 Quick Sort

    https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...

  2. PAT甲1101 Quick Sort

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  3. PAT甲级——A1101 Quick Sort

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

  4. PAT 1101 Quick Sort[一般上]

    1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...

  5. 【刷题-PAT】A1101 Quick Sort (25 分)

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  6. PAT 1101 Quick Sort

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

  7. 1101 Quick Sort

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

  8. Quick Sort(快速排序)

    Quick Sort Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a ...

  9. 1101. Quick Sort (25)

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

随机推荐

  1. RouterOS(ROS)简单限速/单IP限速脚

    暂无评论 有时企业环境,或个人使用环境需要针对不同IP设置较多条不同限速,可以使用以下脚本批量处理后,再针对性的修改. *脚本说明:“2 to 254”定义要设置受限IP的起始,后面“192.168. ...

  2. 霍夫变换Hough

    http://blog.csdn.net/sudohello/article/details/51335237 霍夫变换Hough 霍夫变换(Hough)是一个非常重要的检测间断点边界形状的方法.它通 ...

  3. 删除文件时提示 你需要来自system的权限才能对此文件夹进行更改

    问题描述: 我的计算机是Win7 x64操作系统,在我的计算机的F盘中,不知道什么时候多了个“12e4k69m762nzcgt8zx”这样一个文件夹,应该是某个软件自己创建并留下的文件夹,想删除掉则提 ...

  4. 多级联动下拉菜单--cxSelect

    jquery cxSelect插件 github地址:https://github.com/ciaoca/cxSelect demo地址:http://code.ciaoca.com/jquery/c ...

  5. 简单易懂dubbo入门实例

    一.创建Maven多模块项目 项目结构如下 模块介绍: dubbo-api            ----API接口 dubbo-consumer ----消费者 dubbo-provider     ...

  6. 分享一个js技巧!判断一个变量chat_websocket是否存在。

    注意!!! 判断一个变量chat_websocket是否存在:if( "undefined" == typeof(chat_websocket) || null == chat_w ...

  7. bzoj4403

    组合数学 我好菜啊 想到dp去了... 事实上对于固定长度的数列,我们只用考虑选了哪些数就行了,所以这个就是$C(n+m-1,m-1)$ 也就是$n$个数,划分成$m$段且允许空的方案数 然后变成$\ ...

  8. C#自定义控件 绘制框

    上几张测试的 效果 虽然全是用.net 的绘图库画的,但是手动双缓冲,不会闪烁,感觉还不错,源码开放了,喜欢的拿去扩展吧; 用于撤销的存放图像的数据结构我设置为10个,怕是内存崩了,我看mspaint ...

  9. Flask16 项目结构、flask_script插件

    1 项目结构 需求:易维护.可扩展 1.1 views 处理逻辑和路由映射 C 1.2 models 模型类 M 1.3 templates 模板文件 V 1.4 static 今天文件,如:js.c ...

  10. 使用Cors后台设置WebAPI接口跨域访问

    昨天根据项目组前端开发工程师反映,在浏览器端无法直接使用ajax访问后台接口获取数据,根据他的反映,我查阅了相关跨域的解决方案: 一:使用jsonP,但是jsonP只能使用GET请求,完全不符合我项目 ...