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

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

描述:置换选择排序算法:给出排序数n,排序内存块有限制m,依次判断待排序数,按要求选择序列块
法一:multiset解决(可重复,自动排序)m1,m2。如果大于m1中最小数,选择置换,置换m1中最小数,m1中最小数进ans[index];如果小于m1中最小数,进m2,删除m1中最小数,m1中最小数进ans[index],此数进ans[index+1];如图:在这里插入图片描述
法二:vector+sort 解决,超时,但逻辑简单,可解决重复数。代码没保存。。。。。
法三:有人用map解决,后续试试。
注意:解决超时,一般就不考虑sort了,用自动排序的set,map等解决。

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,m,index = 0,ind = 0,num[100010];
	cin>>n>>m;
	multiset<int> ans[1000],m1,m2;//m1为first runs,m2为second runs 
	for(int i=0;i<n;i++){
		cin>>num[i];
		if(i<m){
			m1.insert(num[i]);
			ans[0].insert(num[i]);//装入runs0 
		}
	}
	for(int i=m;i<n;i++){
		if(num[i] > *m1.begin()){
			ans[index].insert(num[i]);   //装入runs[index]
			m1.erase(m1.begin()) ;
			m1.insert(num[i]);			
		}else{
			m2.insert(num[i]);
			m1.erase(m1.begin());
			ans[index+1].insert(num[i]); //装入runs[index]
		}
        if(m1.size()==0){		//m1中没有可替换的(m2满了),换 
			index++;    //目前runs下标 
			m1 = m2;	
			m2.clear();
		}
	}
    while(ans[ind].size()!=0) ind++;
	for(int i=0;i<ind;i++){	 //打印 
		int st=0;
		for(int nn:ans[i]){
			if(st!=0) cout<<" ";
			cout<<nn;
			st++;
		}
		cout<<endl;
	}
	return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值