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 ...
随机推荐
- Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)
#include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...
- Bicoloring UVA - 10004 二分图判断
\(\color{#0066ff}{题目描述}\) 多组数据,n=0结束,每次一个n,m,之后是边,问你是不是二分图 \(\color{#0066ff}{输入样例}\) 3 3 0 1 1 2 2 0 ...
- 拓扑排序+数学+DP【洛谷P1685】 游览
P1685 游览 题目描述 顺利通过了黄药师的考验,下面就可以尽情游览桃花岛了! 你要从桃花岛的西头开始一直玩到东头,然后在东头的码头离开.可是当你游玩了一次后,发现桃花岛的景色实在是非常的美丽!!! ...
- Eclipse 创建Maven 项目
https://www.cnblogs.com/noteless/p/5213075.html
- linode出现以下报错
Linode Manager 报错 系统重新安装后 解决办法执行 rm -rf ~/.ssh/known_hosts 再继续执行:ssh root@72.14.189.163
- 电路中IC器件电压符号的解释
在电子芯片.运算处理器等集成电路行业中,存在多种电压.常用的的有:VDDQ->The supply voltage to output buffers of a memory chip 存储芯片 ...
- Java中Array与ArrayList的主要区别
1)精辟阐述: 可以将 ArrayList想象成一种"会自动扩增容量的Array". 2)Array([]):最高效:但是其容量固定且无法动态改变: ArrayList: ...
- asp.net core WebAPI学习以及 发布(***入门学习)
A asp.net Core 系列[一]——创建Web应用 asp.net Core 系列[二]—— 使用 ASP.NET Core 和 VS2017 for Windows 创建 Web API a ...
- “随机数”函数的 ES6 实现
生成一个指定长度的数字数组 const getNumArray = len => [...new Array(len).keys()]; const getNumArray = len => ...
- 记录CentOS7.X版本下安装MySQL5.7数据库
记录CentOS7.X版本下安装MySQL5.7数据库 设置rpm下载目录在/opt目录下新建一个目录存放mysql cd /opt sudo mkdir mysql12 下载MySQL的源 wg ...