1101. Quick Sort
来源:互联网 发布:希腊罗马神话 知乎 编辑:程序博客网 时间:2024/06/04 19:58
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:
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<iostream>#include<vector>#include<set>using namespace std;int main(){int n;int cnt=0;cin>>n;vector<int>num(n);vector<bool>pre(n);vector<bool>ans(n);int maxn=0;for(int i=0;i<n;++i){cin>>num[i];if(num[i]>maxn){maxn=num[i];pre[i]=true;}elsepre[i]=false;}maxn=0x7fffffff;for(int i=n-1;i>=0;--i){if(num[i]<maxn){maxn=num[i];if(pre[i]){ans[i]=true;cnt++;continue;}}ans[i]=false;}cout<<cnt<<endl;bool f=false;for(int i=0;i<n;++i)if(ans[i]&&f)cout<<" "<<num[i];else if(ans[i]&&!f){cout<<num[i];f=true;}cout<<endl;return 0;}
- 1101. Quick Sort (25)
- 1101. Quick Sort (25)
- 1101. Quick Sort (25)
- 1101. Quick Sort
- 1101. Quick Sort (25)
- 1101. Quick Sort (25)
- 1101. Quick Sort (25)
- 1101. Quick Sort (25)
- 1101. Quick Sort (25)
- 1101. Quick Sort (25)
- 1101. Quick Sort (25)
- 1101. Quick Sort
- 1101. Quick Sort (25)
- PAT--1101. Quick Sort
- 1101. Quick Sort (25)
- 1101. Quick Sort
- 1101. Quick Sort (25)
- 【PAT】1101. Quick Sort
- spark2.0 英文翻译 Spark Programming Guide
- char_dev
- JNI官方规范中文版——如何使用JNI中的global reference和local reference
- telephony-1(ril模块)
- 华为编码规范之 注释规范
- 1101. Quick Sort
- 回顾2015.3 - 2016.9
- 欢迎使用CSDN-markdown编辑器
- Android入门五(Service)
- ReentrantReadWriteLock与ReentrantLock对比分析
- 如何给ida增加objective-c交叉引用
- 谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词
- [BZOJ1040][ZJOI2008]骑士(树形dp)
- Linux Mint配置