当前位置: 代码迷 >> 综合 >> PTA Replacement Selection
  详细解决方案

PTA Replacement Selection

热度:93   发布时间:2023-12-16 06:00:54.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.

题目大意

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

输入

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或者secondans[k].push_back(first.top());//输出最小值if(a[i]>first.top())//输入数据存入firstfirst.push(a[i]);else				//数据存入secondsecond.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;
}

运行结果

在这里插入图片描述

  相关解决方案