PAT甲级——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 (≤). 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int N, nums[], minN[], maxN[], res = , resNum[];//左边最大值(包括自己),右边最小值
int main()
{
cin >> N;
for (int i = ; i < N; ++i)
cin >> nums[i];
for (int i = ; i < N; ++i)//找到每个位置左边最大的值,不包括自己
maxN[i] = max(maxN[i - ], nums[i - ]);
minN[N - ] = ;
for (int i = N-; i >= ; --i)//找到每个位置右边最小的值,不包括自己
minN[i] = min(minN[i + ], nums[i + ]);
for (int i = ; i < N; ++i)
if (nums[i] > maxN[i] && nums[i] < minN[i])
resNum[res++] = nums[i];
cout << res << endl;
for (int i = ; i < res; ++i)
cout << resNum[i] << (i == res - ? "" : " ");
cout << endl;
return ;
}
最新文章
- CobarClient源码分析(1)
- jquery中的get和set
- boost:进程管理
- NGUI的UIProgressBar使用裁剪方式而不是压缩方式的方法
- HDU 1754 	I Hate It 线段树 单点更新 区间最大值
- (三)映射对象标识符(OID)
- poj2187(未完、有错)
- myBatIs.Net 调用Oracle 存储过程返回游标
- html5学习(二)音频audio
- Setting DPDK+OVS+QEMU on CentOS
- Python大神成长之路: 第一次学习记录
- python day2 练习题
- .net通用防SQL注入漏洞程序(Global.asax方式)
- NancyFX 第八章 内容协商
- HTML入门笔记案例展示(1)
- 多线程——C++
- 简单聊聊WebSocket
- Linux内核分析第四章读书笔记
- (递推)一只小蜜蜂... hdu2044
- Struts通配符映射