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 ...
随机推荐
- 【C# MVC】
http://www.cnblogs.com/powertoolsteam/p/MVC_three.html
- c++内联 inline
内联声明只是建议 ,不一定就会内联. http://www.voidcn.com/blog/u011327981/article/p-5006835.html
- Linux性能指标解释+Oracle性能指标解释
Linux性能指标解释 类别 计数器名称 计数器描述 业界同行认可的资源阀值 memory Free(KB) 可用物理内存数 swap-in/out =0 Swap(KB) 已使用的虚拟内存数.在Li ...
- Java从入门到放弃——03.循环和判断
本文目标 选择结构:if,switch 循环结构:for , foreach ,while,do while 跳出语句:break,continue 1.选择结构 if: if(判断语句){ } s ...
- loj #2025. 「JLOI / SHOI2016」方
#2025. 「JLOI / SHOI2016」方 题目描述 上帝说,不要圆,要方,于是便有了这道题. 由于我们应该方,而且最好能够尽量方,所以上帝派我们来找正方形.上帝把我们派到了一个有 NNN ...
- c++运算符重载-如何决定作为成员函数还是非成员函数
The Decision between Member and Non-member The binary operators = (assignment), [] (array subscripti ...
- docker 部署net core程序 curl访问地址 提示 Connection reset by peer
最近研究netcore 部署到docker上.在参考https://www.cnblogs.com/subendong/p/8992285.html教程之后,部署成功.但是curl访问对应的主机端口地 ...
- HDU6312 Game (多校第二场1004) 简单博弈
Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 黑马学习连接池 druid JdbcTemplate c3p0 池技术
package cn.itcast.jdbctemplate; import org.junit.Test; import org.springframework.jdbc.core.BeanProp ...
- pandas学习3(数据处理)