PAT甲级1101
来源:互联网 发布:ubuntu进入root用户 编辑:程序博客网 时间:2024/06/05 15:02
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<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;int main(){int N;cin >> N;vector<int> v;int temp;while (N--){cin >> temp;v.push_back(temp);}vector<int> min, max;//保存每个数在它之前的这一段中的最大数和在它之后的这一段中的最小数int Min = 1000000001, Max = 0;for (int i = 0; i < v.size(); i++){if (Min > v[v.size()-1-i]){Min = v[v.size()-1-i];}min.push_back(Min);if (Max <v[i]){Max = v[i];}max.push_back(Max);}reverse(min.begin(), min.end());//因为我用的vector,需要逆转下,才能与数字相对应vector<int> vv;for (int i = 0; i < v.size(); i++){if (min[i] == max[i]){vv.push_back(v[i]);}}cout << vv.size() << endl;sort(vv.begin(), vv.end());if (!vv.size())cout << endl;//这个地方是个坑,当0个元素能够成为主元时,还得输出空行,题目没说清elsefor (int i = 0; i < vv.size(); i++){cout << vv[i];if (i != vv.size() - 1)cout << " ";elsecout << endl;}return 0;}
- PAT(甲级)1101
- PAT甲级1101
- PAT-甲级 1101 Quick Sort
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- PAT(甲级)1014
- 命名空间正确的路径
- Bitmap图片压缩
- Java标识符的命名规则
- 算法提高 质因数
- Android开发中HashMap的遍历
- PAT甲级1101
- LibSvm使用说明
- 嵌入式Linux开发 动态改变IGMP版本号
- Ember 翻译——教程十一:添加嵌套路由
- DEDECMS首页获取新闻内容
- pat-bl-1037
- 第一个Python程序
- python+pygame安装教程win8
- 反序数