PAT 1171 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.

Input Specification:
Each input file contains several test cases. The first line gives two positive integers N (≤10 5) and M (

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

#include 
#include
#include
#include
#include
#include
#include 
#include
using namespace std;
const int maxn=100010;
const int INF=1000000000;
int list[maxn];
priority_queue<int,vector<int>,greater<int> > q,temp;
int main(){
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		scanf("%d",&list[i]);
	}
	int count=0;
    
	for(int i=0;i<m;i++){
		q.push(list[i]);
	}
	for(int i=m;i<n;i++){
		if(temp.size()==m){
			q=temp;
			while(!temp.empty()) temp.pop();
		}
		int now=q.top();
		q.pop();
		printf("%d",now);
		if(list[i]>=now){
			q.push(list[i]);
		}
		else{
			temp.push(list[i]);
		}
		if(temp.size()==m) printf("\n");
		else printf(" ");
	}
	int cnt=q.size();
	while(!q.empty()){
		int now=q.top();
		q.pop();
		printf("%d",now);
		cnt--;
		if(cnt!=0) printf(" ");
		else printf("\n");
	}
    cnt=temp.size();
	while(!temp.empty()){
		int num=temp.top();
		temp.pop();
		printf("%d",num);
		cnt--;
		if(cnt!=0) printf(" ");
		else printf("\n");
	}
}

这道题最开始我是建立了一个数组,大小为题目给的m,对里面的数进行排序来做的,然后后面三个测试点超时,我就知道这样子做太笨了,所以我就想到了用priority_queue来代替数组,因为priority_queue是自动排序好的,就省去了我们许多的时间。
需要注意的是建立从小到大的优先队列为priority_queue q,temp;

你可能感兴趣的:(PTA,算法,c++,pat,数据结构)