zoukankan      html  css  js  c++  java
  • A1101. 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.

    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<cstdio>
     2 #include<iostream>
     3 #include<limits.h>
     4 using namespace std;
     5 int num[100001], L[100001] = {0,0}, R[100001] = {0,0};
     6 int main(){
     7     int N, max = -1, cnt = 0, index = -1;
     8     scanf("%d", &N);
     9     for(int i = 0; i < N; i++){
    10         scanf("%d", &num[i]);    
    11     }
    12     for(int i = 0; i < N; i++){
    13         if(num[i] > max){
    14             max = num[i];
    15             L[i] = 1;
    16         }
    17     }
    18     int min = INT_MAX;
    19     for(int i = N - 1; i >= 0; i--){
    20         if(num[i] < min){
    21             min = num[i];
    22             R[i] = 1;
    23         }
    24     }
    25     for(int i = 0; i < N; i++){
    26         if(L[i] && R[i]){
    27             cnt++;
    28         }
    29         if(L[i] && R[i] && index == -1){
    30             index = i;
    31         }
    32     }
    33     if(cnt == 0)
    34         printf("%d
    
    ", cnt);
    35     else
    36         printf("%d
    %d", cnt, num[index]);
    37     for(int i = index + 1; i < N; i++){
    38         if(L[i] && R[i])
    39             printf(" %d", num[i]);
    40     }
    41     cin >> N;
    42     return 0;
    43 }
    View Code

    总结:

    1、与上一题一样的做法。注意无解的情况,应输出2个 ,以表示输出了2行, 否则会报格式错误。

    2、注意min和max的初始值,输入的数字可能很大,最好让min和max在long long的范围足够小和足够大。

  • 相关阅读:
    JSP的动态Include的静态Include
    JAVA观察者模式
    JAVA单例模式
    【转】 linux 安装nginx及编译参数详解
    【转】Linux下nginx配置https协议访问的方法
    【转】./configure && make && make install详解
    【转】linux下如何查看某个软件 是否安装?安装路径在哪
    【转发】查看Linux版本系统信息方法汇总
    【转发】CentOS 7 巨大变动之 systemd 取代 SysV的Init
    【转发】centos 7安装完后出现please make your choice from '1' ......
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8514230.html
Copyright © 2011-2022 走看看