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 ...
随机推荐
- logback -- 配置详解 -- 一 -- <configuration>及子节点
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
- DOS 配置IP地址
@echo off :startIP set /p source=STATIC Y or N or E: echo source:%source% if "%source%" == ...
- Netty权威指南之伪异步I/O编程
为了解决同步阻塞I/O一个链路需要一个线程处理问题,对BIO模型做了优化——后端通过一个线程池处理多个客户端的请求接入,设置线程最大值,防止线程并发接入导致的线程耗尽. 当有新的客户端接入时,将客户端 ...
- 在SELECT DISTINCT 状况下使用 Order BY Newid() 随机数选出记录
在日常作业中,有时候可能是一些活动要抽出得奖人或选出抽查的一些名单, 就常常会使用到 Order BY Newid() 的方式来做随机数选出, 但有可能的状况需是要搭配到 DISTINCT 来选出,这 ...
- iOS开发-VFL初窥
VFL是苹果为了简化Autolayout的编码而推出的抽象语言,在上一篇博客中我们发现如果使用NSLayoutConstraint来添加约束是非常繁琐的. 一个简单的Frame需要添加四个NSLayo ...
- urllib 基础模块
(1) urllib.request:最基本的HTTP请求模块,用来模拟发送请求,就像在浏览器里输入网址然后回车一样(2) urllib.error:异常处理模块,如果出现请求错误,我们可以捕获这些异 ...
- radio的取值
<dd id="pingjia${evaluation.orderItemId }" class="ms-wf clearfix" idx="$ ...
- 深入浅出MFC——消息映射与命令传递(六)
1. 消息分类: 2. 万流归宗——Command Target(CCmdTarget): 3. "消息映射"是MFC内建的一个信息分派机制.通过三个宏(DECLARE_MESSA ...
- js for循环与for in循环的区别
for循环可一遍历数组,而for in循环可以遍历数组和对象 使用for in循环会将Array当成对象遍历,而Array的存取速度明显比Object要快.所以使用for循环遍历数组比for in循环 ...
- Google Analytics访问空白的解决方法
在C:\Windows \System32 \drivers \etc下用记事本打开hosts文档 添加: 74.125.129.112 adwords.google.com 74.125.31.12 ...