• 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.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (≤). Then the next line contains N distinct positive integers no larger than 1. 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

    题意:

      给出一组数字,问这组数字中的一个数是否满足该数字左边的数都比这个数字小,该数字右边的数都比这个数字大。

    思路:

      首先构造两个数组,一个用来存放该数字前面最大的数字,一个用来存放改数字后面最小的数字,然后用原数组与这两个数组比较,看是否满足要求。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6     int n;
     7     cin >> n;
     8     vector<int> minn(n), maxx(n), v(n, 0);
     9     int tempMax = -1;
    10     for (int i = 0; i < n; ++i) {
    11         cin >> v[i];
    12         if (v[i] > tempMax) {
    13             maxx[i] = tempMax;
    14             tempMax = v[i];
    15         } else {
    16             maxx[i] = tempMax;
    17         }
    18     }
    19     int tempMin = INT_MAX;
    20     for (int i = n - 1; i >= 0; --i) {
    21         if (v[i] < tempMin) {
    22             minn[i] = tempMin;
    23             tempMin = v[i];
    24         } else {
    25             minn[i] = tempMin;
    26         }
    27     }
    28 
    29     vector<int> ans;
    30     for (int i = 0; i < n; ++i) {
    31         if (v[i] > maxx[i] && v[i] < minn[i]) ans.push_back(v[i]);
    32     }
    33     sort(ans.begin(), ans.end());
    34     bool isFirst = true;
    35     cout << ans.size() << endl;
    36     for (int i : ans)
    37         if (isFirst) {
    38             cout << i;
    39             isFirst = false;
    40         } else {
    41             cout << " " << i;
    42         }
    43     cout << endl;
    44     return 0;
    45 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    软工实践个人总结
    第06组 Beta版本演示
    第06组 Beta冲刺(5/5)
    第06组 Beta冲刺(4/5)
    第06组 Beta冲刺(3/5)
    第06组 Beta冲刺(2/5)
    第06组 Beta冲刺(1/5)
    第06组 Alpha事后诸葛亮
    第06组 Alpha冲刺(6/6)
    第06组 Alpha冲刺(5/6)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12795444.html
Copyright © 2020-2023  润新知