將輸入小到大排序

若為奇數個,則輸出中間值

若為偶數個,則輸出中間兩數(包含兩數)所有的值

 

PS: quicksort 會 TLE 因為 worst case 會到 O( n^2 )

 

#include<iostream>
#include<queue>

using namespace std;

int input[1000001];

void radixSort();
int t,temp;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    while(cin>>t){

        for(int i=0;i<t;i++)
            cin >> input[i];

        radixSort();
        if(t%2)
            cout << "A=" << input[(t+1)/2-1]<<endl;
        else{
            temp = input[t/2-1];
            cout << "A=" << temp;
            temp++;

            while(temp <= input[t/2]){
                cout << "、" << temp;
                temp++;
            }

            cout<<endl;
        }
    }

    return 0;
}


void radixSort()
{
    int i, j, k;

    //find max number
    int max_num = 0;
    for(i = 0; i<t; i++)
        if(input[i] > max_num)
            max_num = input[i];

    //bucketSort
    queue<int> buckets[10];

    j = 1;
    while(max_num/j){
        for(i = 0; i<t; i++)
            buckets[ (input[i]/j) %10].push(input[i]);

        k = 0;
        for(i = 0; i<10; i++)
            while(!buckets[i].empty()){
                input[k++] = buckets[i].front();
                buckets[i].pop();
            }

        j*=10;
    }
}
 

arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()