【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;
}
相关推荐
charlie11451419113 小时前
现代Qt开发教程(新手篇)1.10——进程
开发语言·c++·qt·学习
生物信息与育种13 小时前
全基因组重测序及群体遗传与进化分析技术服务指南
人工智能·深度学习·算法·数据分析·r语言
AI人工智能+电脑小能手13 小时前
【大白话说Java面试题】【Java基础篇】第23题:ConcurrentHashMap的底层原理是什么
java·开发语言·算法·哈希算法·散列表·hash
葳_人生_蕤13 小时前
hot100——回溯和DFS、BFS
算法·深度优先
Eloudy13 小时前
Steane码的稳定子的生成元集计算过程
算法
MegaDataFlowers13 小时前
快速算法验证流水线
算法
Aaron158813 小时前
27DR/47DR/67DR技术对比及应用分析
人工智能·算法·fpga开发·硬件架构·硬件工程·信息与通信·基带工程
alphaTao13 小时前
LeetCode 每日一题 2026/4/27-2026/5/3
算法·leetcode
wj30558537813 小时前
CMake 项目切换 Ninja 构建问题排查记录
c++
汉克老师14 小时前
GESP2025年6月认证C++五级( 第一部分选择题(1-8))
c++·链表·线性筛·最大公约数·gesp5级·gesp五级·埃氏筛