zoukankan      html  css  js  c++  java
  • 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
      
    •  1 #include<stdio.h>
       2 #include<string>
       3 #include<iostream>
       4 #include<string.h>
       5 #include<sstream>
       6 #include<vector>
       7 #include<map>
       8 using namespace std;
       9 bool is[100100];
      10 int main()
      11 {
      12     int n;
      13     int MAX = -1,MIN = 1000000000,tem;
      14     vector<int> vv,re;
      15     scanf("%d",&n);
      16     for(int i = 0;i <n;++i)
      17     {
      18         scanf("%d",&tem);
      19         if(tem > MAX)
      20         {
      21             is[i] = 1;
      22             MAX = tem;
      23         }
      24         vv.push_back(tem);
      25     }
      26     for(int i = n-1;i >=0;--i)
      27     {    
      28         if(vv[i] < MIN)
      29         {
      30             MIN = vv[i];
      31             if(is[i]) re.push_back(vv[i]);
      32         }
      33         else is[i] = 0;
      34     }
      35     printf("%d
      ",re.size());
      36     for(int i = re.size() - 1;i >= 0;--i)
      37     {
      38         if(i == re.size() - 1) printf("%d",re[i]);
      39         else printf(" %d",re[i]);
      40     }
      41     printf("
      ");
      42     return 0;
      43 }
  • 相关阅读:
    求数组中的最小子数组,时间复杂度o(n),java
    第四周进度条
    四则混合运算3
    软件工程作业3
    《构建之法》第三周阅读笔记
    第三周学习进度
    学习进度01
    构建之法阅读笔记01
    构建之法问题
    随机生成题目运算
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5218873.html
Copyright © 2011-2022 走看看