pat1101. 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 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 5Sample 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)的更多相关文章
- PAT1101:Quick Sort
1101. Quick Sort (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Peng There is a ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- PAT (Advanced Level) 1101. Quick Sort (25)
树状数组+离散化 #include<cstdio> #include<cstring> #include<cmath> #include<map> #i ...
- PAT甲题题解-1101. Quick Sort (25)-大水题
快速排序有一个特点,就是在排序过程中,我们会从序列找一个pivot,它前面的都小于它,它后面的都大于它.题目给你n个数的序列,让你找出适合这个序列的pivot有多少个并且输出来. 大水题,正循环和倒着 ...
- A1101 Quick Sort (25 分)
一.技术总结 这里的一个关键就是理解调换位置排序是时,如果是元主,那么它要确保的条件就只有两个一个是,自己的位置不变,还有就是前面的元素不能有比自己大的. 二.参考代码 #include<ios ...
- 【PAT甲级】1101 Quick Sort (25 分)
题意: 输入一个正整数N(<=1e5),接着输入一行N个各不相同的正整数.输出可以作为快速排序枢纽点的个数并升序输出这些点的值. trick: 测试点2格式错误原因:当答案为0时,需要换行两次
- PAT甲级——1101 Quick Sort (快速排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90613846 1101 Quick Sort (25 分) ...
- PAT_A1101#Quick Sort
Source: PAT A1101 Quick Sort (25 分) Description: There is a classical process named partition in the ...
- 【刷题-PAT】A1101 Quick Sort (25 分)
1101 Quick Sort (25 分) There is a classical process named partition in the famous quick sort algorit ...
随机推荐
- 实现基于dotnetcore的扫一扫登录功能
第一次写博客,前几天看到.netcore的认证,就心血来潮想实现一下基于netcore的一个扫一扫的功能,实现思路构思大概是web端通过cookie认证进行授权,手机端通过jwt授权,web端登录界面 ...
- 怎么用Shell连接VirtualBox Linux虚拟机,在Mac电脑上
问题描述 由于VirtualBox采用桥接的方式连接网络,所以不能在Mac上直接访问虚拟机. 解决思路和办法 由于不能直连,但VirtualBox支持端口转发功能,可以设定转发规则,绑定宿主机和虚拟机 ...
- jmeter:dubbo接口测试
最近工作中接到一个需求,需要对一个MQ消息队列进行性能测试,测试其消费能力,开发提供了一个dubbo服务来供我调用发送消息. 这篇博客,介绍下如何利用jmeter来测试dubbo接口,并进行性能测试. ...
- ScriptableObject
什么是ScriptableObject? 点击查看Unity官网的描述 直译过来就是“脚本化对象”,换言之这类作为存储结构化的数据来使用,并写入Unity的资源.asset文件去存储一组数据,取用的时 ...
- kuangbin专题七 HDU4027 Can you answer these queries? (线段树)
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- 黑马JavaScript学习一 BOM之Window对象定时器功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 微信小程序传数组(Json字符串)到Java后端
一:小程序端: wxml中代码: <!--index.wxml--> <view> <view> <button bindtap="onShow&q ...
- linux中脚本权限问题以及win下使用telnet测试linux端口
一个脚本叫up,执行脚本报错如下: -bash: ./up: Permission denied 解决: chmod +rx up 在执行,OK了. /************************ ...
- 及时从数据库中取得数据填放进Form表单的多选框中
#写上以下代码就不用担心数据库添加了数据而不能及时获取了 def __init__(self, *args, **kwargs): #每次创建Form1对象时执行init方法 super(Form1, ...
- python3 发送邮件添加附件
from email.header import Headerfrom email.mime.application import MIMEApplicationfrom email.mime.mul ...