ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1101 Quick Sort (25 分)

2021-03-06 18:34:05  阅读:257  来源: 互联网

标签:Sort 25 int larger than Quick pivot line its


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 (≤). Then the next line contains N distinct positive integers no larger than 1. 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
   
#include<bits/stdc++.h>
using namespace std;
const int maxn=0x3fffffff;
vector<int> leftMax;
vector<int> rightMin;
vector<int> A;
int main(){
    int n;
    scanf("%d",&n);
    leftMax.resize(n);
    rightMin.resize(n);
    A.resize(n);
    for(int i=0;i<n;i++){
        scanf("%d",&A[i]);
    }
    leftMax[0]=0;
    for(int i=1;i<n;i++){
        leftMax[i]=max(leftMax[i-1],A[i-1]);
    }
    rightMin[n-1]=maxn;
    for(int i=n-2;i>=0;i--){
        rightMin[i]=min(rightMin[i+1],A[i+1]);
    }
    int count=0;
    set<int> s;
    for(int i=0;i<n;i++){
        if(A[i]>leftMax[i]&&A[i]<rightMin[i]){
            s.insert(A[i]);
            count++;
        }
    }
    printf("%d\n",count);
    for(set<int>::iterator it=s.begin();it!=s.end();it++){
        if(it!=s.begin()){
            printf(" %d",*it);
        }
        else{
            printf("%d",*it);
        }
    }
    printf("\n");
    return 0;
}

 

标签:Sort,25,int,larger,than,Quick,pivot,line,its
来源: https://www.cnblogs.com/dreamzj/p/14491756.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有