【PAT甲级真题】- Insert or Merge (25)

题目来源

Insert or Merge (25)

Insertion or Heap Sort (25)这题几乎一致,只是一个是单步堆排,一个是单步归并

Heap Sort 这道的题解点这里

注意点:

  • 输出排多一步的序列

题目描述

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration,

insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts

it there. It repeats until no input elements remain.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is

considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1

sublist remaining.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some

sorting method, can you tell which sorting method we are using?

输入描述:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=100). Then in the next line, N integers are

given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target

sequence is always ascending. All the numbers in a line are separated by a space.

输出描述:

For each test case, print in the first line either "Insertion Sort" or "Merge Sort" to indicate the method used to obtain the partial result.

Then run this method for one more iteration and output in the second line the resulting sequence. It is guaranteed that the answer is

unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the

line.

输入例子:

复制代码
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

输出例子:

复制代码
Insertion Sort
1 2 3 5 7 8 9 4 6 0

思路简介

由于要么插入排序要么归并排序

所以用单步插入排序判断是不是插入排序就能行

判断完成后用单步排序函数去排序

单步插入排序:很简单,插入排序第 k 步时,前面的元素都有序,第 k 个元素只要比第 k-1 个元素小就交换,一直换到不能换为止就完成了一次单步插入排序

单步归并排序:第 k 次合并 2 k 2^k 2k 个元素,用 sort(v.begin()+i,v.begin+min(i+k,n)) 来排序,其中 k 就是要合并的元素数量,每次乘以 2 ,i 是遍历的下标

注意,单步排序有可能排序一次之后还会与原序列相同,题目的意思是一直排序直到与给出的第二个序列不同为止

遇到的问题

  1. 单步排序后任然与第二个序列相同 wa 了一次

代码

cpp 复制代码
/**
 * https://www.nowcoder.com/pat/5/problem/4037
 * 模拟
 */
#include<bits/stdc++.h>
using namespace std;

vector<int> insertionSort(vector<int>v,int k){//从k开始排一步
    //cout<<k<<'\n';
    if(k>=v.size())return v;
    while(k-1>=0){
        if(v[k]<v[k-1]){
            swap(v[k],v[k-1]);
        }
        k--;
    }
    return v;
}

vector<int> mergeSort(vector<int>v,vector<int>end){//meerge直接处理了,不用在主函数中单步merge,因为已经完成了判断
    int k=2,len=v.size();
    while(v!=end){
        for(int i=0;i<len;i+=k){
            sort(v.begin()+i,v.begin()+min(i+k,len));
        }
        k*=2;
    }
    for(int i=0;i<len;i+=k){
        sort(v.begin()+i,v.begin()+min(i+k,len));
    }
    return v;
}

void solve(){   
    int n;cin>>n;
    vector<int>start(n),end(n),tmp(n);
    for(int i=0;i<n;++i)cin>>start[i];
    for(int i=0;i<n;++i)cin>>end[i];
    int k=0;
    tmp=start;
    while(k<n){
        if(tmp==end){
            cout<<"Insertion Sort\n";
            while(tmp==end)tmp=insertionSort(tmp,k++);
            for(int i=0;i<n;++i)cout<<tmp[i]<<' ';
            return;
        }
        tmp=insertionSort(tmp,k++);
    };
    cout<<"Merge Sort\n";
    tmp=mergeSort(start,end);
    for(int i=0;i<n;++i)cout<<tmp[i]<<' ';
}   

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //fstream in("in.txt",ios::in);cin.rdbuf(in.rdbuf());
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}
相关推荐
心抵鹊16 小时前
归并排序之翻转对(hard)
数据结构·算法
白山编程大哥16 小时前
Java 集合算法:从排序、查找到底层原理的实战指南
java·python·算法
_Twink1e16 小时前
openJiuwen 实训营 Day 1 · WorkSwarm 入门教程
开发语言·c++·openjiuwen
shehuiyuelaiyuehao16 小时前
算法34,位运算符操作,总结
算法
Navigator_Z16 小时前
LeetCode //C - 1224. Maximum Equal Frequency
c语言·算法·leetcode
dong_junshuai17 小时前
每天一个开源项目#87 fmt:24.5K Stars 的 C++ 格式化核心
c++·开源·github
Navigator_Z17 小时前
LeetCode //C++ - 1226. The Dining Philosophers
c语言·算法·leetcode
FOORIR17 小时前
3D视觉+AI客流系统怎么做:从Sensor Pipeline到数据事件引擎
算法
雪的季节18 小时前
Multisim14.0的详细中文安装步骤【附安装包】
c++
Persistent的粽子!18 小时前
C++:类与对象(二)
开发语言·c++