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. spoj Longest Common Substring

    Longest Common Substring SPOJ - LCS 题意:求两个串的最长公共子串 /* 对一个串建立后缀自动机,用另一个串上去匹配 */ #include<iostream& ...

  2. 主要介绍JavaEE中Maven Web 项目的结构及其它几个小问题

    先说下本篇随笔的目录. 1.介绍windows中环境变量Path与ClassPath的区别. 2.可能导致命令行运行javac编译成功,但 java命令 + 所要执行的类的类名 无效的原因. 3.介绍 ...

  3. 阅读GFS的一点总结

    这是我第一次阅读学术论文,文章中充斥的一些学术名词给我的阅读带来了一些困难,因为此前没有接触过这方面的内容,在同学的帮助下,查阅了一些资料,终于对GFS有了一点认识,写出这一些感悟,文章措辞不严谨之处 ...

  4. oracle ,mysql,postgres jdbc配置文件

    #db mysql #jdbc.driver=com.mysql.jdbc.Driver #jdbc.url=jdbc:mysql://localhost:3306/mysql?&useUni ...

  5. CF708B Recover the String 构造

    For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 an ...

  6. [WebShow系列] 评委打分端现场操作方法

    前期准备: 在活动现场,组委会为每一个评委准备了打分相关东西: A.一个移动打分设备(平板电脑,或评委自己手机,或电脑也行); B.(可选)纸质的打分清单和笔.此清单中有打分细则,上场选手清单及打分处 ...

  7. 安装maven时遇到的问题

    配置好环境变量和本地仓库后,在命令行里输入mvn compile,可以显示成功,但是mvn install后就一直失败.显示如下错误: 在idea中显示如下错误: 其实即使显示编译成功,但并没有真正的 ...

  8. #!/usr/bin/env python 是什么

    #!/usr/bin/env python 这一行注释经常出现在Python脚本的第一行.这一行到底用来干嘛的呢?原因很简单,任何Python脚本执行都需要靠Python解释器.这一行注释正是告诉计算 ...

  9. 本地访问Vmware虚机Web网站

    情况:公司是域环境,Vmware网络设置的是NAT连接模式,里外装的都是Windows,虚机网络IP地址是自动获取的. 查看: 1.虚机Ping本地的IP地址可以Ping通: 2.本地Ping虚机的I ...

  10. Go语言基础之11--Goroutine

    一.创建goroutine 1)在go语言中,每一个并发的执行单元叫做一个goroutine: 2)当一个程序启动时,其主函数即在一个单独的goroutine中运行,一般这个goroutine是主go ...