zoukankan      html  css  js  c++  java
  • PAT甲1101 Quick Sort

    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 (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:

    5
    1 3 2 4 5
    

    Sample Output:

    3
    1 4 5

    题意:

    给一串序列,问有多少个数满足快排的privot,即左边的都小于他,右边的都大于他。

    思路:

    又想当然了....

    首先我们知道快排有一个性质,每一轮排序后privot都会在最后应该在的位置上。

    所以我们可以对原来的序列排个序,排好序的序列如果某个数和原来的一样,那这个数就有可能是privot。

    只是有可能而已。比如序列5 1 3 2 4,虽然3的位置对了,但是他是不满足的。所以我们还应该要去找前i个数中的最大值,如果没有大于当前的,他才是真正可行的。至于后面有没有小于他的,其实是不用比较的。因为比如num[i]最终应该在i的位置,说明他是序列中第i大的,前面既然没有大于他的,说明1-i-1大的都在他前面了,后面是不会再有比他小的数了。

     1 #include <iostream>
     2 #include <set>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <map>
    10 using namespace std;
    11 typedef long long LL;
    12 #define inf 0x7f7f7f7f
    13 
    14 int n;
    15 const int maxn = 1e5 + 5;
    16 LL num[maxn], tmp[maxn], ans[maxn];
    17 
    18 int main()
    19 {
    20     scanf("%d", &n);
    21     for(int i = 1; i <= n; i++){
    22         scanf("%lld", &num[i]);
    23         tmp[i] = num[i];
    24     }
    25     sort(tmp + 1, tmp + 1 + n);
    26     int cnt = 0, m = 0;
    27     for(int i = 1; i <= n; i++){
    28         if(tmp[i] == num[i] && num[i] > m){
    29             ans[cnt++] = num[i];
    30         }
    31         if(num[i] > m){
    32             m = num[i];
    33         }
    34     }
    35     printf("%d
    ", cnt);
    36     for(int i = 0; i < cnt; i++){
    37         if(i)printf(" ");
    38         printf("%lld", ans[i]);
    39     }
    40     printf("
    ");
    41     return 0;
    42 }
    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 (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:

    5
    1 3 2 4 5
    

    Sample Output:

    3
    1 4 5
  • 相关阅读:
    晚绑定是继承机制的根源,是编程语言应对可变性的机制,是一种分离机制
    软件变化性的应对之道
    松耦合-软绑定
    软件的依赖关系:类图关系:is-a has-a use-a
    UML总结:UML用于建模描述结构和行为
    Thinkphp模板中函数的使用
    thinkphp模版调用函数方法
    60.0.1(64位)windows版 uploadify使用有问题
    uploadify在火狐下上传不了的解决方案,java版(Spring+SpringMVC+MyBatis)详细解决方案
    Firefox浏览器怎么安装adobe flash player插件
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9869334.html
Copyright © 2011-2022 走看看