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

    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.

    作者: CAO, Peng
    单位: Google
    时间限制: 200 ms
    内存限制: 64 MB
    代码长度限制: 16 KB

    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.

     思路:快速排序的主元,左边的元素都小于它,右边的元素都大于它;因此输入数据后,遍历,预先算出对于每个数字,左边最大的数和右边最小的数,每个数字只要大于左边最大的,小于右边最小的是主元。

    注意数字有9位,用long型

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <vector>
     6 using namespace std;
     7 const int maxn = 100005;
     8 
     9 long List[maxn],LMax[maxn],RMin[maxn];
    10 
    11 int main(){
    12     fill(LMax,LMax+maxn,0);
    13     fill(RMin,RMin+maxn,9999999999);
    14     
    15     
    16     int n; cin >> n;
    17     long lm=0;
    18     for(int i=0;i<n;i++){
    19         scanf("%ld",&List[i]);
    20         LMax[i]=lm;
    21         if(List[i]>lm) lm=List[i];
    22     }
    23     long rm=9999999999;
    24     for(int i=n-1;i>=0;i--){
    25         RMin[i]=rm;
    26         if(List[i]<rm) rm=List[i];
    27     }
    28     vector<long> ans;
    29     for(int i=0;i<n;i++){
    30         if(List[i]>LMax[i]&&List[i]<RMin[i]){
    31             ans.push_back(List[i]);
    32         }
    33     }
    34     sort(ans.begin(), ans.begin());
    35     
    36     printf("%lu
    ",ans.size());
    37     for(int i=0;i<ans.size();i++){
    38         if(i!=0) printf(" ");
    39         printf("%ld",ans[i]);
    40     }
    41 }
  • 相关阅读:
    2-5
    2-3
    2-2
    2-1
    1-1
    实验6-1 求数组及其下标
    实验4-2 关于求阶乘的运算
    作业 3-5 switch语句的应用
    作业3-6 查询水果单价
    作业3-4 判断是不是闰年
  • 原文地址:https://www.cnblogs.com/lokwongho/p/9935031.html
Copyright © 2011-2022 走看看