PAT 1101 Quick Sort
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 5
Sample Output:
3
1 4 5
分析
这道题就是求一列数中比左边所有数大,比右边所有数小的数是哪些,其实只需要创建两个vector MAX, MIN.
MAX[i]表示vec[i]左边的所有数中最大的数,
MIN[i]表示vec[i]右边的所有数中最小的数。
其中初始化MAX[0]=0,MIN[N-1]=1000000001;
MAX[i]=max(MAX[i-1], vec[i-1]);
MIN[i]=min(MIN[i+1], vec[i+1]);
如果vec[i]比MAX[i]大,比MIN[i]小,就满足条件。
特别提醒最后还要输出换行,不然有个测试点过不了
#include<iostream> //最后要输出换行符
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
int main(){
int N;
cin>>N;
vector<int> vec(N,0);
for(int i=0; i<N; i++)
cin>>vec[i];
vector<int> Max(N,0), Min(N,0);
Max[0]=0; Min[N-1]=1000000001;
for(int i=1; i<N; i++)
Max[i]=max(Max[i-1], vec[i-1]);
for(int i=N-2; i>=0; i--)
Min[i]=min(Min[i+1], vec[i+1]);
vector<int> ans;
for(int i=0; i<N; i++)
if(vec[i]>Max[i]&&vec[i]<Min[i])
ans.push_back(vec[i]);
sort(ans.begin(), ans.end());
cout<<ans.size()<<endl;
for(int i=0; i<ans.size(); i++)
i==0?cout<<ans[i]:cout<<" "<<ans[i];
cout<<endl;
return 0;
}
PAT 1101 Quick Sort的更多相关文章
- PAT 1101 Quick Sort[一般上]
1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...
- 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 (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分) ...
- PAT 甲级 1101 Quick Sort
https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...
- 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 ...
- 1101 Quick Sort(25 分
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT (Advanced Level) 1101. Quick Sort (25)
树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...
- PAT甲题题解-1101. Quick Sort (25)-大水题
快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...
随机推荐
- 浅析Java开发模式—Model1、Model2和三层
"解耦"的思想一直是我们倡导的,但在实际项目中怎样去做?这是需要我们去好好思考的.下面以Model1.Model2.三层为切入点,对比下去了解解耦的思想. Model1 使用JSP ...
- 关于OpenFileDialog的使用 2(转)
关于OpenFileDialog的使用 (2010-03-25 12:51:33) 标签: 杂谈 分类: WinForm 1. OpenFileDialog控件有以下基本属性 InitialDirec ...
- typescript 入门例子 Hello world——ts就是一个宿主机语言
安装 TypeScript TypeScript 的命令行工具安装方法如下: npm install -g typescript 安装完成之后,就有了 tsc 命令.编译一个 TypeScript 文 ...
- bzoj3661
网络流/贪心 网络流做法是对于每一列,如果一个兔子下一天继续可以存在,那么连一条容量为1的边,然后设立一个中转站,来控制可以换的数量,容量限制l.时限100s,能跑过去我的太慢了,一个点100s 正解 ...
- MyBatis高级查询 一对一映射
drop database if exists simple; create database simple; use simple; drop table if exists sys_user; c ...
- sql数据库CHECKDB时报x个分配错误和x个一致性错误
--1.在SQL查询分析器中执行以下语句:(注以下所用的POS为数据库名称,请用户手工改为自己的数据库名) use pos dbcc checkdb --2.查看查询结果,有很多红色字体显示,最后结果 ...
- cookie封装函数(添加,获取,删除)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 9.12NOIP模拟题
NOIP 2017 全假模拟冲刺 hkd 题目名称 Spfa 走楼梯缩小版 滑稽 题目类型 传统 传统 传统 ...
- bzoj1121[POI2008]激光发射器SZK(结论)
1121: [POI2008]激光发射器SZK Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 916 Solved: 759[Submit][Sta ...
- Vue 页面回退参数被当作字符串处理
当时情景是这样的,我从A页面跳到B页面时会传一个Boolean类型的参数,当B跳到C,再从C返回B的时候,控制台打印发现参数还在,可是判断怎么都不起作用,后来发现,当页面返回的时候,默认将参数变成了字 ...