PAT_A1101#Quick Sort

Source:

PAT A1101 Quick Sort (25 分)

Description:

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

Keys:

  • 快速排序

Attention:

  • 最终位置上的元素不一定都是枢轴
  • 第二行的换行符不能少(无聊-,-)

Code:

 1 /*
 2 Data: 2019-07-11 21:35:25
 3 Problem: PAT_A1101#Quick Sort
 4 AC: 46:34
 5
 6 题目大意:
 7 给定序列,统计能够作为枢轴的元素的个数,并递增输出
 8
 9 基本思路:
10 快排中,枢轴的位置是最终位置,
11 因此排序后遍历各元素是否在最终的位置上,
12 同时确定该元素是否满足枢轴的条件
13 */
14 #include<cstdio>
15 #include<vector>
16 #include<algorithm>
17 using namespace std;
18
19 int main()
20 {
21 #ifdef ONLINE_JUDGE
22 #else
23     freopen("Test.txt", "r", stdin);
24 #endif // ONLINE_JUDGE
25
26     int n,Max=0,Min=1e9+10;
27     scanf("%d", &n);
28     vector<int> s(n),m(n),lmax(n),rmin(n),ans;
29     for(int i=0; i<n; i++)
30         scanf("%d", &s[i]);
31     lmax[0]=Max;
32     for(int i=1; i<n; i++)
33     {
34         if(s[i-1] > Max)
35         {
36             lmax[i]=s[i-1];
37             Max = s[i-1];
38         }
39         else
40             lmax[i]=Max;
41     }
42     rmin[n-1]=Min;
43     for(int i=n-2; i>=0; i--)
44     {
45         if(s[i+1] < Min)
46         {
47             rmin[i]=s[i+1];
48             Min = s[i+1];
49         }
50         else
51             rmin[i]=Min;
52     }
53     m=s;
54     sort(m.begin(),m.end());
55     for(int i=0; i<n; i++)
56         if(s[i]==m[i] && (s[i]>lmax[i] && s[i]<rmin[i]))
57             ans.push_back(s[i]);
58     printf("%d\n", ans.size());
59     for(int i=0; i<ans.size(); i++)
60         printf("%d%c", ans[i],i==ans.size()-1?‘\n‘:‘ ‘);
61     if(ans.size()==0)
62         printf("\n");
63
64     return 0;
65 }

原文地址:https://www.cnblogs.com/blue-lin/p/11172939.html

时间: 2024-10-24 15:52:45

PAT_A1101#Quick Sort的相关文章

快速排序(Quick Sort)的C语言实现

快速排序(Quick Sort)的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对着两部分记录继续进行排序,以达到整个序列有序,具体步骤为 设立枢轴,将比枢轴小的记录移到低端,比枢轴大的记录移到高端,直到low=high停止 分别对枢轴低高端部分再次快速排序(即重复第1步) 重复第1.2步,直到low=high停止 C语言实现(编译器Dev-c++5.4.0,源代码后缀.cpp) 原创文章,转载请注明来自钢铁侠Mac博客http:/

C++: quick sort(快排序)

到目前为止, 我们已经学习到了插入排序, 冒泡排序, 选择排序(selection). 这些排序算法都是comparision based sorting algorithms(即涉及到元素大小的比较来决定元素的先后顺序). 而且算法的时间复杂度上均为O(n^2).但是comparision based 的排序算法远非这几个算法. 而且可以通过利用其它的一些手段(例如divide and conquer technique, 分治法)实现对基于比较的排序算法的时间复杂度降低到O(nlogn).

Quick Sort

(referrence: GeeksforGeeks) Like Merge Sort, Quick Sort is also a divide & conquer problem. It picks an element as pivot and partitions the given array around the picked pivot. Different versions of Picking pivot 1. Always pick first element as pivot

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. G

pat1101. Quick Sort (25)

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

Iterative (non-recursive) Quick Sort

An iterative way of writing quick sort: #include <iostream> #include <stack> #include <utility> using namespace std; void quickSort(int A[], int n) { stack<pair<int, int>> stk; stk.push(make_pair(0, n-1)); while (!stk.empty()

Quick Sort in Java and Erlang

Quick Sort In Java. public static void quickSort(int[] a, int p, int r) {         if (p < r) {             int q = partition(a, p, r);             quickSort(a, p, q);             quickSort(a, q + 1, r);         }     }     private static int partitio

笔试算法题(54):快速排序实现之单向扫描、双向扫描(single-direction scanning, bidirectional scanning of Quick Sort)

议题:快速排序实现之一(单向遍历) 分析: 算法原理:主要由两部分组成,一部分是递归部分QuickSort,它将调用partition进行划分,并取得划分元素P,然后分别对P之前的部分和P 之后的部分递归调用QuickSort:另一部分是partition,选取划分元素P(随机选取数组中的一个元素,交换到数组末尾位置),定义两个标记 值left和right,随着划分的进行,这两个标记值将数组分成三部分,left之左的部分是小于划分元素P的值,left和right之间的部分是大 于等于划分元素P的

排序算法之快速排序(Quick Sort) -- 适用于Leetcode 75 Sort Colors

Quick Sort使用了Divide and Concur的思想: 找一个基准数, 把小于基准数的数都放到基准数之前, 把大于基准数的数都放到基准数之后 Worst case: O(n^2) Average case: O(nlogN) 步骤: 初始的数组 Array a[]: 0 1 2 3 4 5 6 7 8 9 51 73 52 18 91 7 87 73 48 3 基准数: X = a[0] = 51 i 的值: i = 0 j 的值: j = 9 (a.length) Step 1: