1171. Replacement Selection (30)-PAT甲级真题 (置换选择排序 & 优先队列)

题意

输入长度n,再输入一串待排序序列,然后根据置换选择排序的规则输出归并段。

思路

  • 设置正常队列que记录依次插入的数字。
  • 设置优先队列cache1、cache2作为缓冲区。
  • 设置ans数组记录依次输出的数字。
  • cache1的队头一定是最小元素,输出到ans数组,然后判断que队头和ans最大值的关系,决定输入到cache1还是cache2,以此逻辑可以保证cache1.size() + cache2.size() = m
  • 最后当cache2.size() == m时说明cache1已经读空,按要求输出m。

总结

注意优先队列的底层是堆,找最小值方便,插入时间复杂度为O(logn)

题解

#include<bits/stdc++.h>
using namespace std;
int rec[100001], n, m;
int main(){
	cin>>n>>m;
	for(int i = 0; i < n; i ++) scanf("%d", &rec[i]);
	queue<int> que;
	priority_queue<int, vector<int>, greater<int>> cache, cache2;
	for(int i = 0; i < n; i ++) que.push(rec[i]);
	for(int i = 0; i < m; i ++){
		int t = que.front(); que.pop();
		cache2.push(t);
	}
	while(cache2.size() == m){
		cache = cache2;
		while(cache2.size()) cache2.pop();
		vector<int>ans;
		int max_num = cache.top();
		while(cache2.size() != m && que.size()){
			int t = cache.top(); cache.pop();
			ans.push_back(t);
			max_num = t;
			int t2 = que.front(); que.pop();
			if(t2 >= max_num) cache.push(t2);
			else cache2.push(t2);
		}
		while(cache.size()){
			int t = cache.top(); cache.pop();
			ans.push_back(t);
		}
		for(int i = 0; i < ans.size(); i ++)
			printf("%d%c", ans[i], i != ans.size() - 1 ? ' ' : '\n');
	}
	if(cache2.size()){
		vector<int> ans;
		while(cache2.size()){
			int t = cache2.top(); cache2.pop();
			ans.push_back(t);
		}
		sort(ans.begin(), ans.end());
		for(int i = 0; i < ans.size(); i ++)
			printf("%d%c", ans[i], i != ans.size() - 1 ? ' ' : '\n');
	}
	return 0;
}

题目

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 (≤105) 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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值