PTA Replacement Selection

题目

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.

题目大意

给定一串数字,并且提供一个固定大小的“容器”,把一定数目的数字放入容器中,取出其中最小的数字输出,然后将下一个数字放入容器替换最小的数字,再进行筛选最小值并输出。要求每一次输出的最小数字必须比前一个输出的数字大。若是容器中不存在比前一个输出的数字更大的数,那么这一轮替换结束。换行继续输出下一轮的数字。直到所有数字输出完毕。

输入

Each input file contains several test cases. The first line gives two positive integers N ( ≤ 1 0 5 ) N (≤10^5) N(105) and M ( < N / 2 ) M (<N/2) M(<N/2), which are the total number of records to be sorted, and the capacity of the internal memory. Then N N 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.

输出

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.

输入样例

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

输出样例

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

题目分析

本题经过分析,可以采用优先队列的数据结构进行解决。建立两个优先队列符first和second。first用以存储当前替换筛选的数据,second 用以存储后续进入容器时小于当前输出的数据。即first每一次替换,均有可能导致其数据总量减一。当first为空时,即说明了当前一轮替换结束。一轮结束后,将second中的数据依次存入first。因为替换特性,first中的数据每减少一个,second中的数据便会新增一个,因此在second数据转入first时,不必考虑second中数据大于容器中的数据数目。循环处理直至数据输入结束。

代码

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
priority_queue<int,vector<int>,greater<int> > first,second;//优先队列
int a[100005];//存储输入数据
vector<int> ans[50005];//存储每一轮的结果
int main(){
	int n,m,k = 0;
	cin >> n>>m;
	for(int i = 0;i<n;++i)
		scanf("%d",&a[i]);
	for(int i = 0;i<m;++i)
		first.push(a[i]);
	for(int i = m;i<n;++i){//首先将每一个数字输入处理,存入first或者second
		ans[k].push_back(first.top());//输出最小值
		if(a[i]>first.top())//输入数据存入first
			first.push(a[i]);
		else				//数据存入second
			second.push(a[i]);
		first.pop();
		if(first.empty()&&!second.empty()){//将second数据存入first中
			while(!second.empty()){
				first.push(second.top());
				second.pop();
			}
			++k;//轮数加一
		}
	}
	while(!first.empty()){//数字逐个输入队列,但处理未完全
		ans[k].push_back(first.top());
		first.pop();
		if(first.empty()&&!second.empty()){//处理剩余数据second中的数据
			while(!second.empty()){
				first.push(second.top());
				second.pop();
			}
			++k;
		}
	}
	for(int i = 0;i<=k;++i){//输出结果
		for(int j = 0;j<ans[i].size();++j){
			cout << ans[i][j];
			if(j!=ans[i].size()-1) cout << " ";
		}
		cout << endl;
	}
	return 0;
}

运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

registor11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值