本文同步发布在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. Python 爬虫 —— 网页内容解析(lxml)

    0. xpath 语法 找到所有 <img src=....> 图像的链接: xpath = './/img/@src' img_urls = html.xpath(xpath) @修饰节 ...

  2. 【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. bzoj1208Splay

    Splay查前驱后继 小tips:在bzoj上while(scanf)这种东西可以让程序多组数据一起跑 反正没加我就t了 #include<cstdio> #include<iost ...

  4. BZOJ3700: 发展城市

    BZOJ3700: 发展城市 https://lydsy.com/JudgeOnline/problem.php?id=3700 分析: 枚举两个人,先求链交,求到两个端点的时间. 链交求法:求两两\ ...

  5. bzoj 4766: 文艺计算姬 矩阵树定理

    题目: 给定一个一边点数为\(n\),另一边点数为\(m\),共有\(n*m\)条边的带标号完全二分图\(K_{n,m}\) 计算其生成树个数 \(n,m,p \leq 10^{18} ,p为模数\) ...

  6. MySQL 和 InnoDB

    发现一篇总结的很不错的文章,转一下 (原文作者:Draveness   原文链接:https://draveness.me/mysql-innodb) 作为一名开发人员,在日常的工作中会难以避免地接触 ...

  7. 恢复到特定点(时间点、scn、日志序列号),rman不完全恢复

    将数据库.表空间.数据文件等恢复至恢复备份集保存时间中的任何一个时间点/SCN/日志序列(一般是日志挖掘找到误操作点),但须谨慎,操作前一定需要做好备份,具备条件的情况下最好先恢复到异机,避免业务停机 ...

  8. binlog之二:怎么样安全删除mysql下的binlog日志

    删除binlog方法 第一种方法: mysql> show binary logs; 查看mysql bin-log日志,除了这个以外的,其它都可以使用删除.mysql> purge bi ...

  9. SQL连接、嵌套和集合查询---

    SQL连接.嵌套和集合查询 一:连接查询 1 .不同表之间的连接查询 例 查询每个学生及其选修课程的情况. 本查询实际上是涉及Students与Reports两个表的连接操作.这两个表之间的联系是通过 ...

  10. 动态Result配置

    步骤一:建立DynaAction,主要代码如下: package com.asm; public class DynaAction extends ActionSupport { private St ...