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:
- 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 (<= 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:
5
1 3 2 4 5Sample Output:
3
1 4 5 #include<stdio.h>
#include<string>
#include<iostream>
#include<string.h>
#include<sstream>
#include<vector>
#include<map>
using namespace std;
bool is[];
int main()
{
int n;
int MAX = -,MIN = ,tem;
vector<int> vv,re;
scanf("%d",&n);
for(int i = ;i <n;++i)
{
scanf("%d",&tem);
if(tem > MAX)
{
is[i] = ;
MAX = tem;
}
vv.push_back(tem);
}
for(int i = n-;i >=;--i)
{
if(vv[i] < MIN)
{
MIN = vv[i];
if(is[i]) re.push_back(vv[i]);
}
else is[i] = ;
}
printf("%d\n",re.size());
for(int i = re.size() - ;i >= ;--i)
{
if(i == re.size() - ) printf("%d",re[i]);
else printf(" %d",re[i]);
}
printf("\n");
return ;
}
1101. Quick Sort (25)的更多相关文章
- PAT (Advanced Level) 1101. Quick Sort (25)
树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...
- PAT甲题题解-1101. Quick Sort (25)-大水题
快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...
- 【PAT甲级】1101 Quick Sort (25 分)
题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次
- pat1101. Quick Sort (25)
1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...
- PAT甲级——1101 Quick Sort (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分) ...
- PAT甲1101 Quick Sort
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- PAT 1101 Quick Sort[一般上]
1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...
- 1101 Quick Sort(25 分
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- 1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
随机推荐
- 【Mood-5】14条建议,使你的IT职业生涯更上一层楼
升值为企业IT部门的领导者,并非一件易事.从一般大众中脱颖而出,则更不容易. 2013是一个好年头,据专家报告显示,6月所有新工作中,10%来自技术领域.这对于那些希望高升.换岗.跳槽的IT技术人员来 ...
- centos下添加环境变量和启动apache
#修改环境变量文件 vi /etc/profile #文件内容末尾添上 PATH=/usr/local/php/bin:$PATH export PATH #修改后让环境变量生效 source /et ...
- iis8 默认不支持svc解决方法
最近在IIS8中发布WCF服务应用时,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF服务的Managed Handler,按照以下步骤添加后,IIS8即支持WCF服务. 1. 首 ...
- OC 和 swift 小结
1 什么是 OC 语言? OC 语言即面向对象语言,它扩展了 ANSI C 语言,将 SmallTalk 式的消息传递机制加入到 ANSI C 中.它是苹果 OS 和 iOS 以及相关的 API,Co ...
- web前端开发(4)
低权重原则,避免滥用子选择器 CSS选择符是有权重的,当不同选择符的样式设置有冲突时会采用权重高的选择符设置的样式. html标签权重是1,class权重是10,id权重是100 如果css选择符权重 ...
- IDE改为AHCI后系统无法启动的解决办法
1.intel CPU 一.找到HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Msahci,“Start”项值改为“0”. 二.重启,进入b ...
- [书目20131223]Android、iPhone、Windows Phone手机网页及网站设计:最佳实践与设计精粹 - 张亚飞
目录 第I篇 手机版专用网站设计和开发入门篇 第1章 准备创作环境和测试环境 3 1.1 使用Mobile Safari测试网页 4 1.1.1 iOS Simulator安装 5 1.1.2 使用M ...
- Markdown Test
P1 1.1 interview 1.2 invoker show 1.3 Wtf? P2 for example this is a story int main() { printf(" ...
- 转:微信Android客户端架构演进之路
转自: http://www.infoq.com/cn/articles/wechat-android-app-architecture 微信Android客户端架构演进之路 作者 赵原 发布于 20 ...
- css+js 控制幻灯片效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...