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