Source:

PAT A1101 Quick Sort (25 分)

Description:

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

Keys:

  • 快速排序

Attention:

  • 最终位置上的元素不一定都是枢轴
  • 第二行的换行符不能少(无聊-,-)

Code:

 /*
Data: 2019-08-22 19:54:11
Problem: PAT_A1101#Quick Sort
AC: 15:45 题目大意:
给定序列,统计能够作为枢轴的元素的个数,并递增输出 基本思路:
遍历两次数组,分别记录i位置左边的最大值和右边的最小值
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e5+;
int s[M],ma[M],mi[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &s[i]);
s[]=s[];
s[n+]=s[n];
int Max=s[];
for(int i=; i<=n; i++)
{
if(s[i-]>Max)
Max=s[i-];
ma[i]=Max;
}
int Min=s[n];
for(int i=n; i>=; i--)
{
if(s[i+]<Min)
Min=s[i+];
mi[i]=Min;
}
vector<int> pivot;
for(int i=; i<=n; i++)
if(s[i]>=ma[i] && s[i]<=mi[i])
pivot.push_back(s[i]);
sort(pivot.begin(),pivot.end());
printf("%d\n", pivot.size());
for(int i=; i<pivot.size(); i++)
printf("%d%c", pivot[i],i==pivot.size()-?'\n':' ');
if(!pivot.size())
printf("\n"); return ;
}

最新文章

  1. 里面的div怎么撑开外面的div,让高度自适应
  2. 使用safe-rm替代rm
  3. 【转载】Hadoop可视化分析利器之Hue
  4. .NET DLL 保护措施应用实例(百度云批量保存工具)
  5. Spring boot 1.3.5 RELEASE 官方文档中文翻译--Part2:新手入门
  6. AT-Activity
  7. 给Chrome和Firefox添加js脚本作为插件的方法
  8. 炫酷吊炸天的nodeppt
  9. 10. linux输入子系统/input 设备【转】
  10. 一起学爬虫——urllib库常用方法用法总结
  11. HBuilder 模拟器
  12. [转]EOS智能合约 &amp; 私链激活 &amp; 基本操作
  13. Android语音识别
  14. WPF如何为程序添加splashScreen(初始屏幕)
  15. 关于jQ的Ajax操作
  16. Java 内存分配及垃圾回收机制初探
  17. cxGrid使用汇总
  18. 基于 Dapper 的一个 DbUtils
  19. 1px 下划线solid的问题
  20. opencv-写入AVI视频文件

热门文章

  1. SVN迁移Gitlab步骤
  2. Linux中的特殊权限s、t、i、a
  3. 2015 ACM-ICPC 亚洲区上海站 A - An Easy Physics Problem (计算几何)
  4. python学习笔记:接口开发——PythonWEB框架之Flask
  5. 如何将英文版的Firefox添加中文版语言包
  6. Shell判断某文件夹下是否存在xxx开头的字符串
  7. 为什么javaBean要有get/set方法的设计
  8. 条件选择case
  9. SQL中to_char方法的应用
  10. Win7下设置一键关闭所有程序的功能