【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;
}
相关推荐
不爱吃炸鸡柳2 小时前
6道经典算法题详解:从排序到链表,覆盖面试高频考点
算法·链表·面试
wfbcg2 小时前
每日算法练习:LeetCode 3. 无重复字符的最长子串 ✅
算法·leetcode·职场和发展
_日拱一卒2 小时前
LeetCode:矩阵置零
java·数据结构·线性代数·算法·leetcode·职场和发展·矩阵
穿条秋裤到处跑2 小时前
每日一道leetcode(2026.04.10):三个相等元素之间的最小距离 I
算法·leetcode
nlpming2 小时前
OpenClaw 代码解析
算法
程序员学习随笔2 小时前
C++条件变量(一):从轮询到唤醒 —— 条件变量的设计动机与基础用法
c++·线程并发
学习永无止境@2 小时前
MATLAB中矩阵转置
算法·matlab·fpga开发·矩阵
七颗糖很甜2 小时前
雨滴谱数据深度解析——从原始变量到科学产品的Python实现【下篇】
python·算法·pandas
nlpming2 小时前
OpenClaw system prompt定义
算法