1101. Quick Sort (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

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

提交代码

学习网址:http://blog.csdn.net/xyt8023y/article/details/48437945

题目要求找出序列中的所有x,使得x满足≥前面所有的数,≤后面所有的数,这样的x称为快排中的主元。

为了快速的判断,显然我们需要x左侧的最大值和右侧的最小值,而且他们一直在变动,一个思路是用两个vector或者数组记录每个位置之前最大值、之后最小值,称为maxBefore和minBehind,它们的实现逻辑如下:

①第一个元素没有左侧元素,因此maxBefore[0]=-1作为初始化条件,这样就保证了必然满足。

②最后一个元素没有右侧元素,因此minBehind[N-1]=INF(注意INF>10的9次方)。

③对于中间部分,只需要定义max和min两个变量实时判断赋值,对于maxBefore,在输入过程中完成;minBehind通过一次反向遍历建立。

建立好了两个表,就可以对每个元素进行查询,满足了存入res,如果res规模大于0,则先输出规模,再输出排序后的序列;否则输出0,因为序列为空,因此需要空一行,也就是两个回车符。

想到了就比较简单了。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int before[],behind[],num[];
#define inf 1000000005
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j;
int maxbefore=-,minbehind=inf;
for(i=; i<n; i++)
{
scanf("%d",&num[i]);
before[i]=maxbefore;
if(maxbefore<num[i]){
maxbefore=num[i];
}
}
for(i=n-; i>=; i--)
{
behind[i]=minbehind;
if(minbehind>num[i]){
minbehind=num[i];
}
}
queue<int> q;
for(i=; i<n; i++)
{
if(num[i]>before[i]&&num[i]<behind[i])
{
q.push(num[i]);
}
}
printf("%d\n",q.size());
if(!q.empty())
{
printf("%d",q.front());
q.pop();
}
while(!q.empty())
{
printf(" %d",q.front());
q.pop();
}
printf("\n");
return ;
}

自己的做法:借助归并排序,排序过程中被交换过的元素,肯定不符合题意。其他都对,只是最后一个样例超时。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int num[],temp[];
map<int,bool> mm;
void Merge(int l,int r,int n)
{
int i=l,j=r,k=l;
bool hav=false;
while(i<r&&j<=n)
{
if(num[i]<num[j])
{
temp[k++]=num[i++];
}
else
{
hav=true;
mm[num[j]]=true;
temp[k++]=num[j++];
break;
}
}
if(hav)//
{
while(i<r&&j<=n)
{
if(num[i]<num[j])
{
mm[num[i]]=true;
temp[k++]=num[i++];
}
else
{
mm[num[j]]=true;
temp[k++]=num[j++];
}
}
while(i<r){
mm[num[i]]=true;
temp[k++]=num[i++];
}
}
while(i<r){
temp[k++]=num[i++];
}
while(j<=n)
{
temp[k++]=num[j++];
}
for(i=l;i<=n;i++){
num[i]=temp[i];
}
}
void MergeSort(int l,int r)
{
if(l<r)
{
int mid=(l+r)/;
MergeSort(l,mid);
MergeSort(mid+,r);
Merge(l,mid+,r);
}
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j;
for(i=; i<n; i++)
{
scanf("%d",&num[i]);
mm[num[i]]=false;
}
MergeSort(,n-);
queue<int> q;
for(i=; i<n; i++)
{
if(!mm[num[i]])
{
q.push(num[i]);
}
}
printf("%d\n",q.size());
if(!q.empty())
{
printf("%d",q.front());
q.pop();
}
while(!q.empty())
{
printf(" %d",q.front());
q.pop();
}
printf("\n");
return ;
}

pat1101. Quick Sort (25)的更多相关文章

  1. PAT1101:Quick Sort

    1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...

  2. 1101. Quick Sort (25)

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

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

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

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

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

  5. A1101 Quick Sort (25 分)

    一.技术总结 这里的一个关键就是理解调换位置排序是时,如果是元主,那么它要确保的条件就只有两个一个是,自己的位置不变,还有就是前面的元素不能有比自己大的. 二.参考代码 #include<ios ...

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

    题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次

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

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

  8. PAT_A1101#Quick Sort

    Source: PAT A1101 Quick Sort (25 分) Description: There is a classical process named partition in the ...

  9. 【刷题-PAT】A1101 Quick Sort (25 分)

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

随机推荐

  1. Java连接Hbase异常

    Exception in thread "main" org.apache.hadoop.hbase.client.RetriesExhaustedException: Faile ...

  2. unity网络延迟

    using UnityEngine; using System.Collections; public class Test_Ping : MonoBehaviour { public string ...

  3. 【图灵学院10】高并发之java线程池源码分析

    1. 提纲 1)线程池的模块结构 2)示例&原理解析 2. 问题 1)线程池包含哪些东西 2)线程池的运作原理 3)调度线程池的运作原理 4)线程池怎么实现FixRate,FixDelay,他 ...

  4. Java泛型<>内各种参数的异同

    先说下本篇随笔主要涉及到的东西(参考Java编程思想一书): 1.说明 List<Fruit> 与 List<Apple> 之间为什么是非继承关系. 2.由 1 引出的问题说明 ...

  5. 解决vue-cli相对路径问题 about css assert path ,two Solution(css路径的问题解决方案) #179

    https://github.com/vuejs/vue-cli/issues/179

  6. Spark大数据处理 之 从WordCount看Spark大数据处理的核心机制(2)

    在上一篇文章中,我们讲了Spark大数据处理的可扩展性和负载均衡,今天要讲的是更为重点的容错处理,这涉及到Spark的应用场景和RDD的设计来源. Spark的应用场景 Spark主要针对两种场景: ...

  7. p2p-如何拯救k8s镜像分发的阿喀琉斯之踵?

    K8s的出现为PaaS行业的发展打了一针兴奋剂,Docker+k8s的技术路线已经成为了容器云的主流.尤其针对大流量,大弹性的应用场景来说,k8s将其从繁杂的运维.部署工作中彻底拯救出来.然而事情往往 ...

  8. php 读取excel文件

    首先下载插件PHPExcel (PHPExcel-1.8),以tp5框架为例,将该文件放在verdor文件夹下.然后引入IOFactory文件. 1.读取文件的部分内容(用于固定格式) public ...

  9. 各种Helper代码

    1.读取XML文件 /// <summary> /// 读取XML配置文件类 /// </summary> public class XmlHelper { private s ...

  10. Spring注解实现定时功能以及Quartz定时器

    一:Spring注解实现--------->Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz 1:maven配置: <!-- quartz--> <d ...