PAT(甲級)2020年春季考試 7-4 Replacement Selection

7-4 Replacement Selection (30分)

When the input is much too large to fit into memory, we have to do external sorting instead of internal sorting. One of the key steps in external sorting is to generate sets of sorted records (also called runs) with limited internal memory. The simplest method is to read as many records as possible into the memory, and sort them internally, then write the resulting run back to some tape. The size of each run is the same as the capacity of the internal memory.算法

Replacement Selection sorting algorithm was described in 1965 by Donald Knuth. Notice that as soon as the first record is written to an output tape, the memory it used becomes available for another record. Assume that we are sorting in ascending order, if the next record is not smaller than the record we have just output, then it can be included in the run.數組

For example, suppose that we have a set of input { 81, 94, 11, 96, 12, 99, 35 }, and our memory can sort 3 records only. By the simplest method we will obtain three runs: { 11, 81, 94 }, { 12, 96, 99 } and { 35 }. According to the replacement selection algorithm, we would read and sort the first 3 records { 81, 94, 11 } and output 11 as the smallest one. Then one space is available so 96 is read in and will join the first run since it is larger than 11. Now we have { 81, 94, 96 }. After 81 is out, 12 comes in but it must belong to the next run since it is smaller than 81. Hence we have { 94, 96, 12 } where 12 will stay since it belongs to the next run. When 94 is out and 99 is in, since 99 is larger than 94, it must belong to the first run. Eventually we will obtain two runs: the first one contains { 11, 81, 94, 96, 99 } and the second one contains { 12, 35 }.測試

Your job is to implement this replacement selection algorithm.this

Input Specification:

Each input file contains several test cases. The first line gives two positive integers $N (≤10​^5​​)$ and $M (<N/2)$, which are the total number of records to be sorted, and the capacity of the internal memory. Then N numbers are given in the next line, all in the range of int. All the numbers in a line are separated by a space.spa

Output Specification:

For each test case, print in each line a run (in ascending order) generated by the replacement selection algorithm. All the numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.code

Sample Input:

13 3
81 94 11 96 12 99 17 35 28 58 41 75 15

Sample Output:

11 81 94 96 99
12 17 28 35 41 58 75
15

題目限制:

image.png

題目大意:

給定一長度爲N的序列,假設如今內存大小爲M(<N/2),那麼如今須要使用外部排序的置換選擇排序算法對利用該內存空間進行排序輸出。blog

算法思路:

其實就是模擬外部排序的過程,題目舉的例子算是說的比較詳細了,模擬的步驟以下:排序

  • 一、假設待排序的序列存儲在unsorted隊列中,前M個元素已經加入到當前輪次隊列currentRun待處理,那麼只要當前隊列不空就進行2~3的循環,不然轉4
  • 二、在當前輪的部分有序序列currentRun中出隊一個元素,並添加到result數組中進行暫存,而後再從unsorted隊列中出隊一個元素,若是大於此前進入result的元素,進入當前輪次的排序隊列currentRun中,不然就加入下一輪次的隊列nextRun中。
  • 三、判斷currentRun是否爲空,若是是,說明得進入下一輪排序,將result進行輸出並狀況,將nextRun賦值給currentRun並清空,
  • 四、檢測當前輪次排序隊列currentRun是否所有處理完畢,若是沒有就添加進result數組中,並輸出result元素。而後判斷下一輪次的隊列nextRun是否有元素須要處理,若是有就加入到currentRun進行排序,而後依次輸出currentRun的每個元素便可。

注意點:

  • 一、因爲每次都得在當前輪次隊列中獲取最小的元素,那麼使用優先隊列是最方便的
  • 二、對於測試點1和測試點2內存超限的狀況,有多是沒有及時釋放nextRun所形成的,這裏將nextRun設置爲vector就必須得釋放,可是若是是優先隊列的話,就會出現該問題。
  • 三、判斷當前輪爲空得在當前輪次處理完畢以後才行,不然測試點1錯誤或者測試點2格式錯誤

提交結果:

image.png

AC代碼:

#include<cstdio>
#include<vector>
#include<queue>
#include<unordered_map>

using namespace std;

priority_queue<int,vector<int>,greater<int> > currentRun;//當前輪次
queue<int> unsorted;//待排序部分
vector<int> nextRun;//暫存下一輪數字
vector<int> result;// 保存每一輪結果

int main(){
    int N,M;
    scanf("%d %d",&N,&M);
    // 先將前M個數字加入到當前輪次中
    for(int i=0;i<M;++i){
        int num;
        scanf("%d",&num);
        currentRun.push(num);
    }
    // 剩餘N-M個數字加入到待排序序列中
    for(int i=M;i<N;++i){
        int num;
        scanf("%d",&num);
        unsorted.push(num);
    }
    while(!unsorted.empty()){
        // 只要還有數字須要排序
        int temp = currentRun.top();
        currentRun.pop();
        result.push_back(temp);
        // 出隊未排序序列元素
        int num = unsorted.front();
        unsorted.pop();
        if(num>result[result.size()-1]){
            // 比上一次出隊元素大,在當前輪次
            currentRun.push(num);
        }else{
            // 在下一輪次
            nextRun.push_back(num);
        }
        // 判斷當前輪爲空得在當前輪次處理完畢以後才行,不然測試點1錯誤或者測試點2格式錯誤
        if(currentRun.empty()) {
            //當前輪爲空,須要輸出result,並進行下一輪
            for (int i = 0; i < result.size(); ++i) {
                printf("%d", result[i]);
                if (i < result.size() - 1) printf(" ");
            }
            printf("\n");
            for (int i:nextRun) {
                currentRun.push(i);
            }
            nextRun.clear();
            result.clear();
        }
    }
    // 須要排序的都已經爲空了,判斷當前輪次還有沒有處理完畢的
    if(!currentRun.empty()) {
        while(!currentRun.empty()){
            // 當期輪還有數字沒有輸出
            int temp = currentRun.top();
            currentRun.pop();
            result.push_back(temp);
        }
        for(int i=0;i<result.size();++i){
            printf("%d",result[i]);
            if(i<result.size()-1) printf(" ");
        }
        printf("\n");
    }
    if(!nextRun.empty()){
        // 下一輪不空
        for(int i : nextRun){
            currentRun.push(i);
        }
        // 輸出
        while(!currentRun.empty()){
            int temp = currentRun.top();
            currentRun.pop();
            if(currentRun.empty()){
                // 最後一個元素
                printf("%d",temp);
            }else{
                printf("%d ",temp);
            }
        }
    }
    return 0;
}
相關文章
相關標籤/搜索