PAT甲级:1089 Insert or Merge (25分)

题干

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?

Input Specification:

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.

Output Specification:

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 resuling 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.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6

思路

根据两个排序的特性,我们不难发现:

  1. 插入排序中,总会是前半段有序,后半段无序。
  2. 归并排序中,则是一段一段的有序。

由于题目保证唯一解,我们不考虑两者交叉的情况。实际上是两者在很多情况下是无法分清的。

所以首先利用两者的特性,找到第一个次序不对的位置。再继续调查剩下的序列情况,如果与原数列都相同,说明是插入排序。否则一定是归并排序。

插入排序只需要用sort函数处理0到刚刚找到的位置即可。

而归并排序就直接对数据进行排序,等到某一步与给出的第二列数据相同,就再进行一步即可。

code

#include <iostream>
#include <vector>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void merge(vector<int>& sorted, vector<int>& init){
int k = 1, flag = 1;
while(flag){
flag = 0;
int i;
if(sorted != init) flag = 1;
k *= 2;
for(i = 0; i + k < init.size(); i += k) sort(init.begin() + i, init.begin() + i + k);
sort(init.begin() + i, init.end());
}
}
int main(int argc, char** argv) {
int n = 0, temp_split = 0;
scanf("%d", &n);
vector<int> init(n), sorted(n);
for(int i = 0; i < n; i++) scanf("%d", &init[i]);
for(int i = 0; i < n; i++) scanf("%d", &sorted[i]);
for(int i = 1; i < n; i++){
if(sorted[i] < sorted[i-1]){
temp_split = i;
break;
}
}
for(int i = temp_split; i < n; i++){
if(init[i] != sorted[i]){
printf("Merge Sort\n");
merge(sorted, init);
for(auto it = init.begin(); it != init.end(); it++){
if(it != init.begin()) printf(" ");
printf("%d", *it);
}
return 0;
}
}
printf("Insertion Sort\n");
sort(sorted.begin(), sorted.begin() + temp_split + 1);
for(auto it = sorted.begin(); it != sorted.end(); it++){
if(it != sorted.begin()) printf(" ");
printf("%d", *it);
}
return 0;
}

PAT甲级:1089 Insert or Merge (25分)的更多相关文章

  1. PAT甲级1089. Insert or Merge

    PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...

  2. 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)

    题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...

  3. 1089 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  4. PAT Advanced 1089 Insert or Merge (25) [two pointers]

    题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...

  5. 1089 Insert or Merge (25分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  6. PTA 09-排序2 Insert or Merge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge   (25分) According to ...

  7. PAT 甲级 1083 List Grades (25 分)

    1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...

  8. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  9. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

随机推荐

  1. gradle 混合编程java、scala、kotlin、groovy

    众所周知,java是基于JVM的开发语言,但能够在JVM上运行的开发语言不仅仅有java,目前还有很热门的kotlin(kotlin不仅仅只能开发Android).scala.groovy等等.目前国 ...

  2. GVS案例分享|乘新时代姑苏舫号,体验匠心智能控制

    水,是苏州的灵魂,串起苏州的古与今.动与静.金鸡湖景区位于苏州工业园区,总面积11.5平方公里,其中水域面积7.4平方公里. 新时代姑苏舫号,是金鸡湖景区极具苏式特征且规格超高的游览船型.船体分为上下 ...

  3. Linux学习笔记:Linux命令之文件处理命令

    文件处理命令 touch 命令名称:touch 执行权限:所有用户 功能描述:创建空文件 语法:touch [文件名] touch创建文件的时候命名不推荐存在空格,如下面的情况 1touch prog ...

  4. Django(65)jwt认证原理

    前言 带着问题学习是最有目的性的,我们先提出以下几个问题,看看通过这篇博客的讲解,能解决问题吗? 什么是JWT? 为什么要用JWT?它有什么优势? JWT的认证流程是怎样的? JWT的工作原理? 我们 ...

  5. 搭建简单模型训练MNIST数据集

    # -*- coding = utf-8 -*- # @Time : 2021/3/16 # @Author : pistachio # @File : test1.py # @Software : ...

  6. java处理方法的多个返回值

    我第一次接触到元组是在java编程思想这本书中,当时我正为方法的多个返回值苦恼.因为我之前处理多个返回值方法的时候,要不建一个新的实体类,要不在接收的方法中建立一个对象,返回值之前给其赋值,要不通过异 ...

  7. 循序渐进BootstrapVue,开发公司门户网站(3)--- 结合邮件发送,收集用户反馈信息

    在我们公司门户网站里面,如果有需要,我们可以提供一个页面给用户反馈信息,以便获得宝贵的用户信息反馈或者一些产品咨询的记录,一般这个结合邮件发送到负责人的邮箱即可.本篇随笔结合后端发送邮件的操作,把相关 ...

  8. docker-compose 部署 Apollo 自定义环境

    Apollo 配置中心是什么: ​ Apollo是携程框架部门研发的开源配置管理中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性. ...

  9. Go语言中底层数组和切片的关系以及数组扩容规则

    Go语言中底层数组和切片的关系以及数组扩容规则 demo package main import ( "fmt" ) func main() { // 声明一个底层数组,长度为10 ...

  10. 使用Let’s Encrypt实现网站https化

    使用 Let's Encrypt 证书和搭配 Nginx 实现网站 https 化. 一.SSL证书获取 由于 Let's Encrypy 申请的 SSL 证书只有三个月的有效期,为了实现自动续期,使 ...