【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;
}
相关推荐
geovindu22 分钟前
go: Iterative Algorithms
开发语言·后端·算法·golang·迭代算法
稚南城才子,乌衣巷风流2 小时前
块状链表:数据结构详解与实现
数据结构·链表
Zachery Pole2 小时前
CCF-CSP备战NO.7【队列】
算法
闪电悠米3 小时前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展
满天星83035773 小时前
【算法】最长递增子序列(三种解法)
算法
hold?fish:palm3 小时前
kv存储主从复制的设计与实现
c++·redis·后端
啦啦啦啦啦zzzz4 小时前
算法:贪心算法
c++·算法·leetcode·贪心算法
charlie1145141914 小时前
现代C++工程实践 WeakPtr 实战(三):WeakPtrFactory 与「最后成员」惯用法
开发语言·c++·开源项目·现代c++
日拱一卒——功不唐捐5 小时前
红黑树删除(C语言)
c语言·数据结构
清泓y5 小时前
RAG 技术
算法·ai