1101. Quick Sort (25)

题目地址:http://www.patest.cn/contests/pat-a-practise/1101

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

ac代码如下, 这题也太简单了, 然而考场的时候 ,却没ac ? ?

思路就是
1. 该数 左边的数中最大的 要比该数 小
2. 该数 右边的数中最小的 要比该数 大
用数组就行,这题不能在简单了

// 1101. Quick Sort (25)
// http://www.patest.cn/contests/pat-a-practise/1101
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;

#define N 100005
#define Max 1000000001
#define Min -1

int a[N] ;
bool leftr[N] ;
//bool rightr[N] ;

int main()
{
    //freopen("in.txt","r",stdin);
    int n ;
    int i ;
    scanf("%d",&n);
    int minn  = Max ;
    int maxx = Min;
    if(n == 1)
    {
        int tmp ;
        scanf("%d",&tmp);
        printf("1\n%d\n",tmp);
    }else{
        vector<int> ans ;
        ans.clear();

        scanf("%d",&a[0]);
        maxx = a[0] ;
        leftr[0] = true;

        for(i = 1 ;i < n ; i ++)
        {
            scanf("%d",&a[i]) ;
            if(maxx < a[i])
            {
                maxx = a[i];
                leftr[i] = true;
            }else{
                leftr[i] = false;
            }
        }

        minn = a[n-1] ;
        //rightr[n-1] = true;

        for(i = n-1 ; i >= 0 ; i --)
        {
            if(a[i] > minn)
            {
                ;   
            }else{
                minn = a[i] ;
                if(leftr[i] == true)
                {
                    ans.push_back(a[i]);
                }
            }
        }
        int len = ans.size();
        printf("%d\n",len);
        sort(ans.begin(),ans.end());
        if(len > 0)
        {
            printf("%d",ans[0]);
            for(i = 1;i<len;i++)
            {
                printf(" %d",ans[i]);
            }
        }
        printf("\n"); // 这句话 刚开始放上面老是 格式错误 所以放到着居然ac
        //回过头来,就是说此题要求 没有结果 也要打印一个空行
    }
    return 0 ;
}

你可能感兴趣的:(1101. Quick Sort (25))