【PAT】1101. Quick Sort (25)
来源:互联网 发布:架构 指令集 知乎 编辑:程序博客网 时间:2024/05/18 02:49
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分析:假设x为数列中的某个数,如果x大于左边最大的数,小于右边最小的数,那么x就符合入选条件。因此只要用俩个数组,来记录左边最大数,和右边最小数就行了。
陷阱:如果最后的结果是0,那么输出0之后要再输出一个空白行,这绝对是个坑。
代码如下:
#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(int argc, char** argv) {int n,i;cin>>n;vector<int> vec(n);vector<int> left(n);//左边最大的vector<int> right(n); //右边最小的 int max=-1, min = 1000000001;for(i=0; i<n; i++){scanf("%d",&vec[i]);if(vec[i]>max){max = vec[i];}left[i] = max;}if(vec.size()==1){//如果输入只有一个数 printf("1\n%d\n",vec[0]);return 0;}for(i=n-1; i>=0; i--){if(vec[i]<min){ min = vec[i];}right[i] = min;}vector<int> out;int maxLeft, minRight;if(right[0] >= vec[0]){out.push_back(vec[0]);}for(i=1; i<n-1; i++){maxLeft = left[i-1] ;minRight = right[i+1] ;if(vec[i]>maxLeft && vec[i]<minRight){out.push_back(vec[i]);}}if(left[n-1] <= vec[n-1]){out.push_back(vec[n-1]);}sort(out.begin(), out.end());printf("%d\n",out.size());if(out.size()==0){printf("\n"); //要加这个回车,否则不能通过。真是坑。 return 0;} printf("%d",out[0]);for(i=1; i<out.size(); i++){printf(" %d",out[i] );}printf("\n");return 0;}
- 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--1101. Quick Sort
- 【PAT】1101. Quick Sort
- PAT-A 1101. Quick Sort (25)
- PAT A 1101. Quick Sort (25)
- 【PAT】【Advanced Level】1101. Quick Sort (25)
- Pat(A) 1101. Quick Sort (25)
- PAT 1101. Quick Sort (25) 已解决
- PAT 甲级 1101. Quick Sort (25)
- pat-a1101. Quick Sort (25)
- PAT A1101. Quick Sort (25)
- Timer的使用和参数解释
- Android 解决bug:异常提示"ListView cannot be cast to android.widget.BaseAdapter "——诺诺"涂鸦"记忆
- 你不知道的JavaScript--Item16 for 循环和for...in 循环的那点事儿
- const的常见用法
- 深入浅出Java垃圾回收机制1
- 【PAT】1101. Quick Sort (25)
- 内存管理器(六)Glibc malloc 实现(二)--设计模式
- 让APK只包含指定的ABI
- mac下配置php+mysql+Apache+phpMyAdmin环境
- Android面试题目整理与讲解
- 虚继承与虚函数
- 我的android面试经历
- IOS网络笔记--AFNworking练习之文件下载
- TC SRM 671 Div2 1000 BearDestroysDiv2