• 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.

    Input Specification:

    Each input file contains several test cases. The first line gives two positive integers N (≤) and M (<), 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.

    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.

    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

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=100005;
    vector<int> ans[maxn];//存放每一轮run的序列
    int a[maxn];
    int main(){
        priority_queue<int,vector<int>,greater<int> > q[2];
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        int f=0,e=1;
        for(int i=0;i<m;i++){
            q[f].push(a[i]);
        }
        int cnt=0;
        for(int i=m;i<n;i++){
            int minn=q[f].top();
            q[f].pop();
            ans[cnt].push_back(minn);
            if(a[i]>minn){
                q[f].push(a[i]);
            }
            else{
                q[e].push(a[i]);
            }
            if(q[f].size()==0){
                cnt++;
                swap(f,e);
            }
        }
        while(q[f].size()>0){
            ans[cnt].push_back(q[f].top());
            q[f].pop();
        }
        cnt++;
        swap(f,e);
        while(q[f].size()>0){
            ans[cnt].push_back(q[f].top());
            q[f].pop();
        }
        cnt++;
        for(int i=0;i<cnt;i++){
            for(int j=0;j<ans[i].size();j++){
                printf("%d",ans[i][j]);
                if(j<ans[i].size()-1){
                    printf(" ");
                }
            }
            printf("
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    ASP.NET Core Docker部署
    Asp.Net Core 发布和部署(Linux + Jexus )
    Asp.Net Core 发布和部署( MacOS + Linux + Nginx )
    作为一个测试leader平时应该注意哪些方面
    【转】性能测试工具 性能测试如何做?
    【转】Grafana系列教程–Grafana基本概念
    jar包和war包的介绍和区别
    MySQL常用存储引擎
    【参】编程习惯
    【转】性能测试中如何定位性能瓶颈
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14509165.html
Copyright © 2020-2023  润新知