标签:vector 包括 for cin its pre cte color UNC
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:
Hence in total there are 3 pivot candidates.
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.
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.
5
1 3 2 4 5
3 1 4 5
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 using namespace std; 5 int N, nums[100100], minN[100100], maxN[100100], res = 0, resNum[100100];//左边最大值(包括自己),右边最小值 6 int main() 7 { 8 cin >> N; 9 for (int i = 0; i < N; ++i) 10 cin >> nums[i]; 11 for (int i = 1; i < N; ++i)//找到每个位置左边最大的值,不包括自己 12 maxN[i] = max(maxN[i - 1], nums[i - 1]); 13 minN[N - 1] = 9999999999; 14 for (int i = N-2; i >= 0; --i)//找到每个位置右边最小的值,不包括自己 15 minN[i] = min(minN[i + 1], nums[i + 1]); 16 for (int i = 0; i < N; ++i) 17 if (nums[i] > maxN[i] && nums[i] < minN[i]) 18 resNum[res++] = nums[i]; 19 cout << res << endl; 20 for (int i = 0; i < res; ++i) 21 cout << resNum[i] << (i == res - 1 ? "" : " "); 22 cout << endl; 23 return 0; 24 }
标签:vector 包括 for cin its pre cte color UNC
原文地址:https://www.cnblogs.com/zzw1024/p/11366074.html