1101 Quick Sort (25 分)- PAT甲级真题-多种算法及分析

PAT甲级——1101 Quick Sort (快速排序) 1101Quick Sort(25分) There is a classical process namedpartitionin the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the p... 阅读详情

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 (≤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.

Sample Input:

5
1 3 2 4 5

Sample Output:

3
1 4 5

方法1:

最初我采用的是比较朴素的算法,对于每一个元素,分别判断他左边的元素是否全小于他,右边元素是否全部大于他,代码如下,O(n2)的复杂度,但是会导致超时:

注意测试点2,当 count == 0时,还要多输出一个空行。 

#include<cstdio>
const int MAXN = 100010;
int number[MAXN], result[MAXN];

int main(){
    int N,count = 0;
    scanf("%d", &N);
    for(int i = 0 ; i < N; i++){
        scanf("%d", &number[i]);
    }
     for(int i = 0 ; i < N; i++){
        int temp = number[i];
        int flag = 1;
        if( i > 0 ){
            for(int j = i ; j >= 0 ; j--){//首先判断左边的元素
                if(number[j] > temp)
                    {   flag = 0;
                        break;
                        }
            }
            if(flag == 0)
                continue;
            else if(flag == 1){
                for(int k = i ; k < N ; k++){//右边元素
                if(number[k] < temp)
                    {   flag = 0;
                        break;
                        }
            }
            }
        }
        else if( i == 0){
             for(int k = i ; k < N ; k++){//右边元素
                if(number[k] < temp)
                    {   flag = 0;
                        break;
                        }
            }
        }
        if(flag){
            result[count++] = temp;
    }
    }

    printf("%d\n", count);
    if(count == 0)
        printf("\n");
    else{
    for(int i = 0; i< count; i++){
        if(i == count - 1){
            printf("%d", result[i]);
        }
        else{
        printf("%d ", result[i]);
        }
    }
    return 0;
    }
}

方法2:

采用排序的方法,假如一个数是pivot的话,那么经过排序,他的位置是不会变的(因为左边的数全部小于他,右边的数全部大于他),但是要注意,还有别的情况可能导致位置不变,即两边大于他或小于他的数的数量是相等的(比如5 2 3 4 1,虽然3不是pivot但是排序后 1 2 3 4 5,3的位置不变,因为左边5>3 2<3,右边4>3 1<3,5和1交换位置,虽然格局大变,但是3并不能感觉到),因此要多加一个判断条件,left_max,在遍历的同时随时更新left_max,保证pivot > left_max即可。

时间复杂度为排序的复杂度O(nlogn)

#include<cstdio>
#include<algorithm>
using namespace std;

const int MAXN = 100010;
int number[MAXN], sort_number[MAXN], result[MAXN];

int main(){
    int N,count = 0;
    scanf("%d", &N);
    for(int i = 0; i< N; i++){
        scanf("%d", &number[i]);
        sort_number[i] = number[i];
    }

    sort(sort_number, sort_number + N);
    int max = 0;
    for(int i = 0; i < N; i++){
        if(sort_number[i] == number[i] && number[i] > max){
            result[count++] = number[i];
        }
        if(number[i] > max)//找到该元素左边的最大值
            max = number[i];
    }

    printf("%d\n", count);
    if(count == 0)
        printf("\n");
    else{
    for(int i = 0; i< count; i++){
        if(i == count - 1){
            printf("%d", result[i]);
        }
        else{
        printf("%d ", result[i]);
        }
    }
    }
    return 0;
}

参考:https://www.liuchuo.net/archives/1917 

方法3:

受方法2和课本《算法笔记》启发,可以开辟一个数组LeftMax[],从左到右遍历,记录该位置左边的最大值;然后从右到左遍历,记录右边的最小值,当满足 number[i]<=smallest(因为smallest的初值是number[N - 1]可能会有相等情况) && number[i] == LeftMax[i] (LeftMax为本身,说明该值为左边最大值)即为满足条件的值。

时间复杂度O(n)。

#include<cstdio>
#include<algorithm>
using namespace std;

const int MAXN = 100010;
int number[MAXN], result[MAXN];
int LeftMax[MAXN];
int main(){
    int N,count = 0;
    scanf("%d", &N);
    for(int i = 0; i< N; i++){
        scanf("%d", &number[i]);
    }

    LeftMax[0] = number[0];

    for(int i = 0; i < N; i++){
        if(i > 0)
            LeftMax[i] = LeftMax[i - 1];
        if(number[i] > LeftMax[i])
            LeftMax[i] = number[i];
    }
    int smallest = number[N - 1];
    for(int j = N - 1; j >= 0 ; j--){
        if(number[j] <= smallest){
            smallest = number[j];
            if(number[j] == LeftMax[j]){
                result[count++] = number[j];
            }
        }
    }
    sort(result, result + count);
    printf("%d\n", count);
    if(count == 0)
        printf("\n");
    else{
    for(int i = 0; i< count; i++){
        if(i == count - 1){
            printf("%d", result[i]);
        }
        else{
        printf("%d ", result[i]);
        }
    }
    }
    return 0;
}

 

 

Pixhawk RPi CM4 Baseboard 树莓派CM4安装Ubuntu20.04 server 配置ros mavros mavsdk cm4配置ros mavros连接pixhawk4 Pixhawk RPi CM4 Baseboard 阅读详情

相关推荐

【算法】图解A* 搜索算法

A* 算法是启发式搜索算法,是根据Dijkstra算法改进而来。 问题引入 如下图所示,S为起始(start)节点,G为目标(goal)节点。 节点之间连线是两点的路径长度,如A到E的路径长度c(A,E) = 9。 节点旁的h值时当前节点到达目标节点(G)的预估值,如h(A)=15, 表示从当前点A到达目标点G的估计路径长度为15,此处h(x)即为启发函数,启发函数的设计有很多方法,具体可......

zhuhua造轮子的博客 4万+

PAT甲级1101 Quick Sort (25 分)

1101Quick Sort(25分) There is a classical process namedpartitionin the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the p...

Dexter 275

RC4加密算法详解

RC4(也称为 Rivest Cipher 4)是流密码的一种形式,流密码逐字节操作数据流。RC4 流密码是使用最广泛的流密码之一,它通过算法一次一个字节地加密消息,简单并且操作速度快。RC4是一种在电子信息领域加密的技术手段,用于无线通信网络,是一种电子密码,只有经过授权的用户才能享受该服务,使用 64 位或 128 位密钥大小。它通常用于安全套接层 (SSL)、传输层安全 (TLS) 等应用,也用于 IEEE 802.11 无线 LAN 标准。在存储大量的信息时,RC4展现出它的实用性,面对大量的数据,

weixin_43211186的博客 1万+

PAT甲级1101 Quick Sort (25 分)题解

\quad这是一道逻辑题,给了快排的背景。题目大意是给你一个数字序列,找出这个序列里面有多少个数满足两个条件: 它左边的数都小于它 它右边的数都大于它 \quad我们可以暴力求解,时间复杂度为O(n2)O(n^2)O(n2),肯定会超时。正解是将序列存入两个数组a和b,对a排序,比如a={1,2,3,4,5},b={1,3,2,4,5},那么遍历b,若满足下列三个条件则该元素为正确解答的一部...

qq_40438165的博客 1550

YDOOK: ANSYS Maxwell 19 教程7:Maxwell 2D 材料管理 添加新增 永磁材料

使用ANSYS Maxwell 19 添加新增 永磁材料 1. 点击 Add material 按钮后,在左下方的下拉框中下拉选择永磁材料,弹出用材料设置框: 2. 按照下面数据输入 3. 输入 Hc 与 Br/Ap 两项后,其他项的数据将自动计算给出 4. 点击OK后再,为了使得材料属性的完整性,在 Bulk Conductivity 属性行中输入 材料电导率的数值。 ...

YDOOK 1万+

使用SSE流式输出(Javaweb前后端实战)

SSE(Server-Sent Events)是一种在Web应用中实现单向实时通信的技术。通过SSE,服务器可以主动向客户端发送更新,而无需客户端不断发起请求。这对于需要实时更新的应用场景(如聊天应用、实时数据监控等)非常有用。(服务端单向传输客户端,长连接)

记得开心一点嘛的博客 1万+

1101 Quick Sort

1101 Quick Sort

weixin_48448010的博客 162

群晖Hyper Backup还原实战:加密与非加密备份的完整操作指南

本文详细介绍了群晖Hyper Backup在加密与非加密备份场景下的完整还原操作指南。通过实战案例和高级技巧,帮助用户应对硬盘故障或系统崩溃时的数据恢复挑战,特别是加密备份恢复中的关键节点和常见陷阱。

weixin_29323365的博客 443

1101 Quick Sort (25分)[逻辑][快速排序]

By Jalan 文章目录**By Jalan**知识工具需求数学数据结构和算法语言题干输入条件输出条件坑点例子例1输入输出题解第一次思路预期时间复杂度编写用时代码CPP运行用时结尾 知识工具需求 数学 数据结构和算法 语言 题干 快速排序有分类这个步骤,就是选一个数把比他小的放一边大的放另一边. 现在看看有没有这些数 输入条件 N个数<=10^5 每个数不会大于10^9 输出条件 输出左边都比它小右边都比它大的数的个数和数,末尾没空格. 坑点 这题有一个地方我真的没懂,输出完还要打印个换行,要不会有

JalanChao的博客 347

乐视三合一体感摄像头--基本信息及windows下部分开发

乐视三合一体感摄像头--基本信息及windows下部分开发Introduction基本信息Windows下使用安装驱动使用openNI使用imageJ调用RGB图像Q:参考资料 Introduction       乐视三合一体感摄像头是乐视对标kinect,做的一款3D体感摄像头;设计目的为用于电视机上体感使用的,包括其USB结构都是为了符合电视机直接操作的,同时是支持相应的游戏操作;但是自从乐视倒闭之后,价格从之前标价899降到了现在140+左

GRF-Sunomikp31 1万+

【原理图PCB专题】Cadence17.4 PCB位号重排与反标

Cadence17.4 PCB位号重排与反标比Cadence16版本更简单一些,那到底如何操作?

一点硬件 1182

1101. Quick Sort

这道题想找到一组数据中满足,左边的数字都小于它,右边的数字都大于它的数字。 最暴力的方法n^2会超时 这道题的Quick Sort实际上已经提示我们了,这道题的重点在于发现符合题目条件的数字在sort之后的位置是相同的,这一点回想quick sort过程就很容易验证,并且保证数组中的数字要大于前面数字的最大值,就会满足题意,具体代码如下:#include<iostream> #include<v

写代码的柯长的博客 326

工业激光器核心参数详解与选型实战:从理论到产线落地(五)

本文深入解析了脉冲激光加工的三大核心参数——脉宽、重复频率和脉冲能量的协同作用机制。通过公式推导和产线案例,揭示了参数设置对加工良率的关键影响:1. 脉宽决定热影响区大小,缩短脉宽可显著提升加工精度;2. 重复频率影响加工速度和热积累效应,需平衡效率与质量;3. 脉冲能量决定材料去除量,过大易导致加工缺陷。文中以ABS塑料打标、模具清洗和微孔钻孔为例,展示了参数优化如何将良率从65%提升至99%,并提供了不同应用场景的参数选型表和调试技巧,强调峰值功率比平均功率更具决定性作用。

weixin_44520581的博客 2422

1101 Quick Sort——PAT甲级

【代码】1101 Quick Sort——PAT甲级。

flyidj的博客 1755

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

【PAT甲级】1101 Quick Sort (25分)

解题过程的小记录,如有错误欢迎指出。 难度:一星(在做完1012后弄懂排序规则后此题并不难) 小导航~题目分析注意点我的解题过程思路bug代码dalao的代码借鉴点 题目分析 注意点 我的解题过程 思路 bug 代码 dalao的代码 全部代码因版权原因不放出来,大家可以自行去柳神博客购买或者参考晴神的上机笔记~ 借鉴点 ...

SpicyChicken的自我修养 5115

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

【PAT甲级 - C++题解】1101 Quick Sort

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 604

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[PAT B1045/A1101]Quick Sort

[PAT B1045/A1101]Quick Sort 本题是PAT乙级1045题和甲级1101题,这里先放英文原题,要做乙级题的同学可以直接看后面的中文原题 题目描述 1101 Quick Sort (25 分)There is a classical process named partition in the famous quick sort algorithm. In this proc...

欢迎来到我的编程小屋 602

1101. Quick Sort (25)-PAT甲级真题

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 pivo...

柳婼 の blog 1274

PAT 甲级 1101  Quick Sort

1101 Quick Sort (25 point(s)) 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 tha...

漫浸天空的雨色的备忘录~ 607

1101 Quick Sort (25 分)

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 pivo

Tropine的博客 191

甲级PAT 1101 Quick Sort (25 分)

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 p...

Joyceyang_999的博客 258

PAT甲级1101、Quick Sort

这个题目一看上去就有动态规划的意思,用数组记录一个元素前的某种信息,以此来达到节省搜索的时间的目的。那么放两个数组来记录就行了,一个记之前最大的,一个记之后最小的,也就是前后都要遍历一遍。一个元素可以作为pivot,那么前面不可以有比它大的,后面不可以有比它小的。要注意的是N可能为0,没有元素给出,这时需要输出的是0,无元素。题中给的是N为positive integer,怎么会为0呢?测试点2是这个东西。我不知道是我理解的问题还是出题的问题。时间复杂度算是O(n)级别,挺好。

geooorge的博客 933

PAT甲级A1101 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 le...

vir_lee的博客 255

甲级PAT 1101 Quick Sort (25 分)(提前打表)

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 p...

Joyceyang_999的博客 322

PAT——1101 Quick Sort 甲级

1101 Quick Sort题目题意代码解析AC代码参考 题目 https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 题意 pivot 中心点,由题可知,快速排序的中心点就是左面都比它小、右边都比它大的位置对应的数字。题目给出一个数字序列,我们需要找出符合要求的中心点 代码解析 测试可知,一个符合要求的中心点,在排序后,其位置是不变的。但如果一个序列是降序序列,其中心位置(n为奇数时)的数的位置也是不变的

ljhsq的博客 241

pat 甲级 排序 A1101 Quick Sort (25分)

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 pivo

weixin_42358356的博客 148
上一篇: 贪心 - PAT B - 1020 月饼 (25 分)
下一篇: PAT - 数学类总结
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值