1101. Quick Sort (25)
2016-09-08 12:24
246 查看
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
#include<iostream> #include<vector> #include<cstdio> #include <algorithm> using namespace std; vector<int>num,res; int N; int maxl[100001],minr[100001]; int main() { int max = -1,min = 0x7fffffff; cin >> N; maxl[0] = max; minr = min; for(int i = 1;i <= N;i++) { int mmm; cin >> mmm; if(mmm > maxl[i-1]) maxl[i]=mmm; else maxl[i]=maxl[i-1]; num.push_back (mmm); } for(int i = N-1;i >= 0;i--) { int mmm = num[i]; if(mmm < minr[i+1]) minr[i]=mmm; else minr[i]=minr[i+1]; } for(int i = 0;i < N;i++) { int tmp = num[i]; int flag = 0; if(tmp < maxl[i] || tmp > minr[i]) flag = 1; if(flag == 0) res.push_back(tmp); } sort(res.begin(),res.end()); printf("%d\n",res.size()); if(res.size() != 0){ printf("%d",res[0]); for(int i = 1;i < res.size();i++) { printf(" %d",res[i]); } } printf("\n"); return 0; }
相关文章推荐
- 25 surefie ways to motivate your team members
- Programming Ability Test学习 3-08. 堆栈模拟队列(25)
- PAT A1024. Palindromic Number (25)
- PAT A1109. Group Photo (25)
- Spring学习笔记25
- PAT 1070. Mooncake (25)
- pat 1021. Deepest Root (25)
- 【PAT-A】1060. Are They Equal (25)
- 25 个在 Web 中嵌入图表的免费资源
- 1043. Is It a Binary Search Tree (25)
- 1025. 反转链表 (25)
- PAT (Advanced) 1048. Find Coins (25)
- web developer tips (25):在iis7下规范URLs和重定向
- 1002. A+B for Polynomials (25)
- PAT_A 1125. Chain the Ropes (25)
- PAT练习(4)-1007. Maximum Subsequence Sum (25)
- 别跟混蛋叫劲儿--金融风暴、老板已逃(下)——网上答疑(25-2)
- PAT 1079. Total Sales of Supply Chain (25)
- 1010. Radix (25)
- PAT (Advanced Level)1032. Sharing (25)