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

题目大意:找出pivot,即其左边都小于它,右边都大于它。并且输出。数据量还挺大的。

//我的想法就是遍历,那么一定会超时的啊。所以想看看大佬的写法

代码来自:https://www.liuchuo.net/archives/1917

#include <iostream>
#include <algorithm>
#include <vector>
#include<stdio.h>
int a[], b[];
using namespace std;
int main() {
int n;
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(a, a + n);//进行排序
int v[], max = , cnt = ;
for (int i = ; i < n; i++) {
if(a[i] == b[i] && b[i] > max)
v[cnt++] = b[i];
if (b[i] > max)
max = b[i];
}
printf("%d\n", cnt);
for(int i = ; i < cnt; i++) {
if (i != ) printf(" ");
printf("%d", v[i]);
}
printf("\n");
return ;
}

思想:很容易证明如果一个数是pivot,那么排序之后其位置肯定是不变的,(不排之前左边都比它小,右边比它大;排完之后,也是这么个情况)

1.另外当special case主元个数为0的时候,是特殊情况。

2.如果最终不输出printf("\n");会有一个格式错误,以后需要注意。

另一种解法,因为pivot拥有的性质是比左边最大的数大,比右边最小的数小

所以一次正向遍历,求出那些比左边最大的数大的数标记为1,反向遍历一次将那些比右边最小数小的&&被标记为1的存进pivot数组,然后倒序输出(从小到大)。

代码来自:https://blog.csdn.net/kid_lwc/article/details/54780837

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std; const int INF = ;
const int N = ;
int a[N];
int la[N];
int flag[N];
int ans[N];
int main()
{
int n;
cin>>n;
int ma=;
for(int i=;i<n;i++){
cin>>a[i];
ma=max(ma,a[i]);
if(ma==a[i]) flag[i]=;//表示比左边最大的大。
}
int mm=INF;
int cnt=;
for(int i=n-;i>=;i--){
mm=min(mm,a[i]);
if(mm==a[i] && flag[i]==)//相等表示最小的是它。
{
ans[cnt++]=a[i];//由大到小放进去
} }
cout<<cnt<<endl;
for(int i=cnt-;i>=;i--){
cout<<ans[i];
if(i) cout<<" ";
}
cout<<endl;
return ;
}

PAT 1101 Quick Sort[一般上]的更多相关文章

  1. PAT 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  2. PAT甲1101 Quick Sort

    1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...

  3. PAT甲级——1101 Quick Sort (快速排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分)   ...

  4. PAT 甲级 1101 Quick Sort

    https://pintia.cn/problem-sets/994805342720868352/problems/994805366343188480 There is a classical p ...

  5. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  6. 1101 Quick Sort

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  7. 1101 Quick Sort(25 分

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...

  8. PAT (Advanced Level) 1101. Quick Sort (25)

    树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...

  9. PAT甲题题解-1101. Quick Sort (25)-大水题

    快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...

随机推荐

  1. thinkphp3.2 实现留言功能

    写一个例子说明一下: 前端:http://www.mmkb.com/zhendao/index/feedback.html <form method="post" actio ...

  2. WP8.1学习系列(第十九章)——事件和路由事件概述

    我们将介绍在使用 C#.Visual Basic 或 Visual C++ 组件扩展 (C++/CX) 作为编程语言并使用 XAML 进行 UI 定义时,针对 Windows 运行时应用的事件的编程概 ...

  3. smarty 双引号中嵌入变量的方法

    1.如果变量中只包含字符.数字.下划线,可以将变量直接写在双引号中,如:"my name is $name" 2.如果带有其它字符,如“.”,则需要将变量用单引号括起来,如:“my ...

  4. Apache服务器SSL双向认证配置

    以Win32版Apache与OpenSSL为例,介绍从创建数字证书到Apache配置的整个过程,希望对读者有所帮助. Apache是目前最流行的WEB服务器之一,借助OpenSSL库,我们可以在Apa ...

  5. Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MariaDB (instead of MySQL)

    https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ This tut ...

  6. Mycat的简易安装及测试

    1.环境 OS版本 CentOS release 6.5 (Final) 64bit DB版本 Mysql 5.6.37 Mycat 1.6 jdk1.7及以上版本 2.实战部署 1.创建用户及用户组 ...

  7. Openstack Nova network

    对于安装设置来说,Openstack就剩下网络这个地方比较复杂. 现在比较喜欢看图 整理了一下网络的资料 1:Nova 网络HA http://unchainyourbrain.com/opensta ...

  8. 【转】失效迭代器(Invalidating Iterators)

      当一个容器变化时,指向该容器中元素的迭代器可能失效.这使得在迭代器变化期间改变容器容易出现问题.在这方面,不同的容器提供不同的保障:vectors: 引起内存重新分配的插入运算使所有迭代器失效,插 ...

  9. 【咸鱼教程】JsZip压缩与解压教程

    引擎版本3.0.6 教程目录一 为什么要用jszip二 如何使用jszip    2.1 下载jszip库    2.2 导入jszip库    2.3 加载和解压zip代码三 Demo源码下载 一 ...

  10. tomcat启动报错:serializer.jar (系统找不到指定的文件。)

    下载最新对应版本的tomcat.移除之前的tomcat.删除原本全部tomcat的目录. 疑似tomcat的lib包被动过.