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.

作者: CAO, Peng
单位: Google
时间限制: 200 ms
内存限制: 64 MB
代码长度限制: 16 KB

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​). Then the next line contains N distinct positive integers no larger than 10​9​​. 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.

思路:快速排序的主元,左边的元素都小于它,右边的元素都大于它;因此输入数据后,遍历,预先算出对于每个数字,左边最大的数和右边最小的数,每个数字只要大于左边最大的,小于右边最小的是主元。

注意数字有9位,用long型

 #include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = ; long List[maxn],LMax[maxn],RMin[maxn]; int main(){
fill(LMax,LMax+maxn,);
fill(RMin,RMin+maxn,); int n; cin >> n;
long lm=;
for(int i=;i<n;i++){
scanf("%ld",&List[i]);
LMax[i]=lm;
if(List[i]>lm) lm=List[i];
}
long rm=;
for(int i=n-;i>=;i--){
RMin[i]=rm;
if(List[i]<rm) rm=List[i];
}
vector<long> ans;
for(int i=;i<n;i++){
if(List[i]>LMax[i]&&List[i]<RMin[i]){
ans.push_back(List[i]);
}
}
sort(ans.begin(), ans.begin()); printf("%lu\n",ans.size());
for(int i=;i<ans.size();i++){
if(i!=) printf(" ");
printf("%ld",ans[i]);
}
}

最新文章

  1. java中文乱码解决之道(八)-----解决URL中文乱码问题
  2. 下载安装JDK,配置环境变量
  3. iOSDate时间格式(转)
  4. Mongodb数据导出工具mongoexport和导入工具mongoimport介绍
  5. Portion of class Throwable’s inheritance hierarchy
  6. iOS开发——数据持久化Swift篇&amp;文件目录路径获取(Home目录,文档目录,缓存目录等)
  7. 【转】关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
  8. hdoj 2147 kiki&#39;s game【博弈】
  9. 每天一点Swift(五)控制器的生命周期和SizeClass
  10. 在ubuntu 上创建 ssl 证书
  11. tinkphp5.0 traits 的引入
  12. 如何判断Linux 是32位还是64位
  13. java 集合学习笔记
  14. 关于下载calipso数据集以及用python将其读到记事本小结
  15. SHELL脚本--tr命令用法和特性全解
  16. js 读取包含特殊字符的属性值
  17. Tcp/IP 的四层模型
  18. 『TensorFlow』读书笔记_进阶卷积神经网络_分类cifar10_下
  19. python第四十一天---作业:简单FTP
  20. JavaSE学习总结(十)—— JDBC与面向对象测试

热门文章

  1. 访问WebServcie遇到配额不足的时候,请增加配额
  2. Jenkins与Git持续集成&amp;&amp;Linux上远程部署Java项目
  3. Django 模板语言 路由 视图
  4. c# 多个事件公用一个相应方法判断事件来源
  5. RoCE vs iWARP
  6. hihoCoder1159 扑克牌
  7. 数据存储(直接写入、NSUserDefaults、NSkeyedArchiver)
  8. 在开发node.js中,关于使用VS2013插件出现一直读取资源的问题
  9. Linux vps服务器国产面板wdcp的安装和使用方法
  10. 数据结构:链表 &gt;&gt; 链表按结点中第j个数据属性排序(冒泡排序法)