【PAT甲级2020年春季考试】7-4 Replacement Selection (30分)(c++)

7-4 Replacement Selection (30分)

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:400 ms
内存限制:64 MB

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 (≤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.

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

题意:

以测例为例(将一组数分为两列)

  • 排序{81,94,11}成{11,81,94},输出最小的11,判断下一个数96(第一列为空)到队列中;
  • 排序{81,94,96},输出最小的81(因为比11大所以可以输出),判断12(第一列中现在是{11,81},12比第一列中的尾值大,放到第二列);
  • 排序{94,96},输出最小的94,判断99(>94)放入队列;
  • 先排序,再输出,最后对下一个数判断放进队列还是放第二列。

思路:

用优先队列实现小顶堆功能,记录队首元素,他就是第一列的尾值,判断下一个数的去向,当队列为空时,输出第一列元素,此时第二列有m个值(因为每次都输出一个值,处理一个值,队列为空,那多余的值都放进第二列了),以此类推。

参考代码:

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

using namespace std;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> arr(n);
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
    priority_queue<int, vector<int>, greater<int>> q;
    vector<int> v, line;
    int index = 0, count = 0, last;
    for (; index < m; index++) q.push(arr[index]);
    while (count != n) {
        last = q.top();
        line.push_back(last);
        q.pop();
        count++;
        if (index < n) {
            if (arr[index] > last) q.push(arr[index++]);
            else v.push_back(arr[index++]);
        }
        if (q.empty()) {
            for (int i = 0; i < line.size(); i++) {
                if (i != 0) printf(" ");
                printf("%d", line[i]);
            }
            printf("\n");
            line.clear();
            for (int i = 0; i < v.size(); i++)q.push(v[i]);
            v.clear();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值