PAT甲1101 Quick Sort
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 5
Sample Output:
3
1 4 5
题意:
给一串序列,问有多少个数满足快排的privot,即左边的都小于他,右边的都大于他。
思路:
又想当然了....
首先我们知道快排有一个性质,每一轮排序后privot都会在最后应该在的位置上。
所以我们可以对原来的序列排个序,排好序的序列如果某个数和原来的一样,那这个数就有可能是privot。
只是有可能而已。比如序列5 1 3 2 4,虽然3的位置对了,但是他是不满足的。所以我们还应该要去找前i个数中的最大值,如果没有大于当前的,他才是真正可行的。至于后面有没有小于他的,其实是不用比较的。因为比如num[i]最终应该在i的位置,说明他是序列中第i大的,前面既然没有大于他的,说明1-i-1大的都在他前面了,后面是不会再有比他小的数了。
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int n;
const int maxn = 1e5 + ;
LL num[maxn], tmp[maxn], ans[maxn]; int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%lld", &num[i]);
tmp[i] = num[i];
}
sort(tmp + , tmp + + n);
int cnt = , m = ;
for(int i = ; i <= n; i++){
if(tmp[i] == num[i] && num[i] > m){
ans[cnt++] = num[i];
}
if(num[i] > m){
m = num[i];
}
}
printf("%d\n", cnt);
for(int i = ; i < cnt; i++){
if(i)printf(" ");
printf("%lld", ans[i]);
}
printf("\n");
return ;
}
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 5
Sample Output:
3
1 4 5
PAT甲1101 Quick Sort的更多相关文章
- 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 ...
- PAT 1101 Quick Sort[一般上]
1101 Quick Sort(25 分) There is a classical process named partition in the famous quick sort algorith ...
- 【刷题-PAT】A1101 Quick Sort (25 分)
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
- PAT 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 ...
- 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甲级——A1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
随机推荐
- nginx反向代理配置(nginx.conf+proxy_set_header)
转载:https://blog.csdn.net/bjsunwei/article/details/62231209 location / { proxy_pass http://test; prox ...
- 关于MySQL数据库
MySQL最流行的关系型数据库管理系统 MySQL官网:http://www.mysql.com MySQL是WEB应用方面最好的RDBMS应用软件之一 RDBMS:Relational Databa ...
- 递归的几个demo
/** * Created by root * Description : 递归函数 */ object RecursionTest { def main(args: Array[String]): ...
- apt-get/dpkg常用指令备查
apt-get install <package> Downloads <package> and all of its dependencies, and installs ...
- phpadmin试用
在Windows或者Linux下mysql安装后默认的密码为空,又当我们又安装了mysql的管理工具phpmyadmin后登陆时 出现“空密码登陆呗禁止(参见允许密码为空)”.不能登录成功 ...
- Oracle和Mysql中mybatis模糊匹配查询区别
1.Oracle AND NAME LIKE '%'||#{name}||'%' 2.Mysql AND NAME LIKE "%"#{name}"%"
- 转:Android开发:使用DDMS Heap进行内存泄露调试
无论怎么小心,想完全避免bad code是不可能的,此时就需要一些工具来帮助我们检查代码中是否存在会造成内存泄漏的地方.Android tools中的DDMS就带有一个很不错的内存监测工具Heap,本 ...
- var_dump出现省略号的问题
xdebug.var_display_max_children=128xdebug.var_display_max_data=512xdebug.var_display_max_depth=5
- 解决Sublime Text 3中文显示乱码问题(转)
解决Sublime Text 3中文显示乱码问题(转) 1.打开Sublime Text 3,按Ctrl+-打开控制行,复制粘贴以下python代码,然后回车运行. 2. 复制并粘贴如下代码: 2.重 ...
- No.1 PyQt学习
由于项目的原因,要学PyQt了.以下是第一天的学习成果 # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui, QtCore class ...