PTA----Quick Sort (25分)

    科技2022-07-13  120

    每个人都有崩溃的时候,就看你的抗压能力到底有多强,如果你的抗压能力强,有办法可以支撑到你能面对并且解决这些困难的话,你就没有问题。                                                                          ----喻言

    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 (≤10​5​​). Then the next line contains N distinct positive integers no larger than 10​9​​. 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 #include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <vector> #include <ctime> #include <cctype> #include <bitset> #include <utility> #include <sstream> #include <complex> #include <iomanip> #include <numeric> #include<unordered_set> #include <climits>//INT_maxnn //#include<bits/stdc++.h> #define PP pair<ll,int> #define inf 0x3f3f3f3f #define INF 0x7fffffff; #define llinf 0x3f3f3f3f3f3f3f3fll #define dinf 1000000000000.0 #define PI 3.1415926 #define LL unsigned int #define wc 1e-8 typedef long long ll; using namespace std; int n, a[100010]; int main() { scanf("%d", &n); vector<int> lmx(n), rmi(n), jg; lmx[0] = 0; rmi[n - 1] = INF; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (i > 0) lmx[i] = max(a[i - 1], lmx[i - 1]); } for (int i = n - 2; i >= 0; i--) rmi[i] = min(a[i + 1], rmi[i + 1]); for (int i = 0; i < n; i++) { if (a[i] > lmx[i] && a[i] < rmi[i]) jg.push_back(a[i]); } printf("%d\n", jg.size()); for (int i = 0; i < jg.size(); i++) { printf("%d", jg[i]); if (i < jg.size() - 1) printf(" "); } printf("\n"); return 0; }

     

    Processed: 0.015, SQL: 8