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 }
  • 相关阅读:
    sql语句游标的写法
    oracle的安装与plsql的环境配置
    oracle中创建表时添加注释
    jsp中Java代码中怎么获取jsp页面元素
    sql模糊查询
    jQuery循环给某个ID赋值
    Codeforces Round #671 (Div. 2)
    TYVJ1935 导弹防御塔
    Educational Codeforces Round 95 (Rated for Div. 2)
    Codeforces Round #670 (Div. 2)
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5218873.html
Copyright © 2011-2022 走看看