PAT A1101. Quick Sort (25)
来源:互联网 发布:python 作图 编辑:程序博客网 时间:2024/05/18 01:35
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.
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:51 3 2 4 5Sample Output:
31 4 5#include <cstdio>#include <algorithm>#include <cstring>#define Max 100010const int MAX = 0x7fffffff;using namespace std;int main(){int n,m=0;int S[Max],L[Max],R[Max],D[Max];scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&S[i]);} L[0]=0;for(int i=1;i<n;i++){L[i]=max(L[i-1],S[i-1]);}R[n-1]=MAX;for(int i=n-2;i>=0;i--){R[i]=min(R[i+1],S[i+1]);}for(int i=0;i<n;i++){if(L[i]<S[i]&&R[i]>S[i]){D[m++]=S[i];}}//sort(D,D+m);printf("%d\n",m);for(int i = 0; i< m; i++){printf("%d",D[i]);if(i<m-1) printf(" ");}printf("\n");system("pause");return 0;}
0 0
- pat-a1101. Quick Sort (25)
- PAT A1101. Quick Sort (25)
- PAT A1101. Quick Sort
- PAT A1101.Quick Sort
- PAT A1101 quick sort (25)
- A1101. Quick Sort (25)
- A1101. Quick Sort (25)
- A1101. Quick Sort
- PAT 1101. Quick Sort (25)
- [PAT]1101. Quick Sort (25)
- 【PAT】1101. Quick Sort (25)
- PAT 1101. Quick Sort (25)
- pat 1101. Quick Sort (25)
- PAT 1101. Quick Sort (25)
- PAT 1101. Quick Sort (25)
- pat 1101. Quick Sort (25)
- PAT-A1101
- PAT-A 1101. Quick Sort (25)
- Linux 自检和 SystemTap
- Java中的语句错误总结(不定期更新)
- .NET 十五岁,谈谈我眼中的.NET
- 【软件测试】功能测试
- 数据结构上机练习(2) 单链表
- PAT A1101. Quick Sort (25)
- Struts2中的路径问题
- Git 在团队中的最佳实践--如何正确使用Git Flow
- Git 实战教程(1)
- linux下shell
- JSON 获取属性值的方法
- linux下调试监控类工具
- C语言中extern的用法
- C++知识点2