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 (≤105) and M (
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
模拟祖师爷Knuth发明的置换选择排序算法。即,内存一次只能放m个数,给定n个数,置换内存中的某个数,这个数必须是不小于先前置换的数;如果找不到这个数,本轮排序完成,进入下一轮排序。重复以上直到所有数字都排好。
1.背景很新颖,但是讲明白了就挺简单的。对比同样背景的算法题,这道题的解释尤其长,相当于难度降低。PAT是c++程序员的语文,读清楚关乎得分。
2.因为要保持解有序,我选用了两个set:将备选的数放在一个set(s1),将即将输出的序列放到另一个set(s2)。
3.每次往s1插入一个数,就要找出符合条件能插入到s2的数,如果没有,就要输出s2,输出后清空s2。我的代码没有满分所以我就不放出来了。放的是别人的满分代码,都注明了来源。
//致谢:https://blog.csdn.net/qq_39072627/article/details/107582244
#include
#include
#include
using namespace std;
int main() {
int N, M; scanf("%d%d", &N, &M);
vector arr(N);
for (int i = 0; i < N; i++) scanf("%d", &arr[i]);
priority_queue, greater> q;
vector v,line;
int index = 0,count=0, last;
for (; index < M; index++) q.push(arr[index]);
while (count != N) {
last = q.top();
line.push_back(last);
q.pop(); count++;
if (index < N) {
if (arr[index] > last) q.push(arr[index++]);
else v.push_back(arr[index++]);
}
if (q.empty()) {
for (int i = 0; i < line.size(); i++) {
if (i != 0) printf(" ");
printf("%d", line[i]);
}
printf("\n"); line.clear();
for (int i = 0; i < v.size(); i++)
q.push(v[i]);
v.clear();
}
}
}
//致谢:https://blog.csdn.net/ziyutongxue/article/details/107582557#comments
#include
#include
#include
//致谢:https://blog.csdn.net/qq_42968686/article/details/107581141
#include
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
priority_queue, greater > Q, q;
vector ans[maxn];
int n, m, a[maxn], tag = 1;
void solve(){
int i, j;
for(i=1;i<=m;++i) Q.push(a[i]);
int cnt = m + 1;
while(cnt <= n){
if(Q.empty()){ //当前队列空
while(!q.empty()){
Q.push(q.top());
q.pop();
}
tag++;
}
if(Q.empty()) return;
int e = Q.top();Q.pop();
ans[tag].push_back(e);
if(a[cnt] < e){
q.push(a[cnt]);
}else{
Q.push(a[cnt]);
}
++cnt;
}
if(Q.empty()){ //当前队列空
while(!q.empty()){
Q.push(q.top());
q.pop();
}
tag++;
}
while(!Q.empty()){
int e = Q.top();Q.pop();
ans[tag].push_back(e);
}
if(Q.empty()){ //当前队列空
while(!q.empty()){
Q.push(q.top());
q.pop();
}
tag++;
}
while(!Q.empty()){
int e = Q.top();Q.pop();
ans[tag].push_back(e);
}
}
int main(){
int t, i, j;
ios::sync_with_stdio(false);
cin>>n>>m;
for(i=1;i<=n;++i) cin>>a[i];
solve();
for(i=1;i<=tag;++i){
sort(ans[i].begin(),ans[i].end());
}
for(i=1;i<=tag;++i){
if(ans[i].empty()) continue;
cout<