【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;
}
相关推荐
绝知此事7 分钟前
【算法突围 01】线性结构与哈希表:后端开发的收纳术
java·数据结构·算法·面试·jdk·散列表
碧海银沙音频科技研究院14 分钟前
通话AEC与语音识别AEC的软硬回采链路
深度学习·算法·语音识别
csdn_aspnet1 小时前
Python 算法快闪 LeetCode 编号 70 - 爬楼梯
python·算法·leetcode·职场和发展
LuminousCPP1 小时前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习
张小姐的猫2 小时前
【Linux】多线程 —— 线程互斥
linux·运维·服务器·c++
AI算法沐枫2 小时前
深度学习python代码处理科研测序数据
数据结构·人工智能·python·深度学习·决策树·机器学习·线性回归
m0_629494734 小时前
LeetCode 热题 100-----26.环形链表 II
数据结构·算法·leetcode·链表
壹号用户4 小时前
用队列实现栈
数据结构·算法
做人求其滴4 小时前
面试经典 150 题 380 274
c++·算法·面试·职场和发展·力扣