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 ...
随机推荐
- selenium 单浏览器模式
如果需要无限制的任意实例化,而不是把实例化后的对象传给另一个函数.方法处理.就需要控制单浏览器,不然每次实例化弹出来一个新浏览器,很占内存 #coding=utf8 from selenium.web ...
- Android ROM 打包记录
android5.1平板或手机应用层一些常用的修改项及目录所在: 1.所以系统应用所在的目录: \alps\packages\apps 2.一些系统属性的定义:并且可以仿写自行定义变量 \alps\b ...
- AES五种加密模式
分组密码在加密时明文分组的长度是固定的,而实用中待加密消息的数据量是不定的,数据格式可能是多种多样的.为了能在各种应用场合安全地使用分组密码,通常对不同的使用目的运用不同的工作模式. 一.电码本模式( ...
- Django 配置
Django 配置 运行 django-admin.py startproject [project-name] 命令会生成一系列文件,在Django 1.6版本以后的 settings.py 文 ...
- 【代码审计】CLTPHP_v5.5.3 前台任意文件上传漏洞
0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chichu/ ...
- UITableView-FDTemplateLayoutCell 学习笔记
本文转载至 http://www.tuicool.com/articles/I7ji2uM 原文 http://everettjf.github.io/2016/03/24/learn-uitabl ...
- Python错误和异常 学习笔记
错误和异常概念 错误: 1.语法错误:代码不符合解释器或者编译器语法 2.逻辑错误:不完整或者不合法输入或者计算出现问题 异常:执行过程中出现万体导致程序无法执行 1.程序遇到 ...
- Android 之 布局训练
1.线性布局 LinearLayout <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- 【盘古分词】Lucene.Net 盘古分词 实现公众号智能自动回复
盘古分词是一个基于 .net framework 的中英文分词组件.主要功能 中文未登录词识别 盘古分词可以对一些不在字典中的未登录词自动识别 词频优先 盘古分词可以根据词频来解决分词的歧义问题 多元 ...
- C#设计模式--工厂方法模式
0.C#设计模式-简单工厂模式 设计模式: 工厂方法模式(Factory Method Pattern) 介绍:简单工厂模式是要在工厂类中通过数据来做个决策,在工厂类中的多个类中实例化出来其中一个要用 ...