PAT甲级:1089 Insert or Merge (25分)
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
思路
根据两个排序的特性,我们不难发现:
- 插入排序中,总会是前半段有序,后半段无序。
- 归并排序中,则是一段一段的有序。
由于题目保证唯一解,我们不考虑两者交叉的情况。实际上是两者在很多情况下是无法分清的。
所以首先利用两者的特性,找到第一个次序不对的位置。再继续调查剩下的序列情况,如果与原数列都相同,说明是插入排序。否则一定是归并排序。
插入排序只需要用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分)的更多相关文章
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT Advanced 1089 Insert or Merge (25) [two pointers]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 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 ...
- PAT 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
随机推荐
- 如何为嵌入式应用选择适当的SSD
如何为嵌入式应用选择适当的SSD Selecting the right SSD for evolving embedded applications 变革涉及技术的每一个要素,闪存也不例外.价格下跌 ...
- 0算法基础学算法 搜索篇第二讲 BFS广度优先搜索的思想
dfs前置知识: 递归链接:0基础算法基础学算法 第六弹 递归 - 球君 - 博客园 (cnblogs.com) dfs深度优先搜索:0基础学算法 搜索篇第一讲 深度优先搜索 - 球君 - 博客园 ( ...
- 手把手使用Python进行语音合成,文字转语音
目录 0. 太长不看系列,直接使用 1. Python调用标贝科技语音合成接口,实现文字转语音 1.1 环境准备: 1.2 获取权限 1.2.1 登录 1.2.2 创建新应用 1.2.3 选择服务 1 ...
- ORACLE中的PL/SQL
一. 1.过程,函数,触发器是pl/sql编写. 2. 过程函数触发器是在Oracle中. 3.pl/sql是非常强大的数据库过 ...
- 同事内推的那位Linux C/C++后端开发同学面试没过......
最近同事内推了一位 Linux C/C++ 后端开发的同学到我们公司面试,我是一面的面试官,很遗憾这位工作了两年的同学面试表现不是很好.我问了如下一些问题: "redis持久化机制,redi ...
- Unity 按空格一直触发Button点击事件的问题
#解决 这是由于Button中Navigation(导航)功能导致的. 将导航设置为None即可. 真是气死我了,我说为什么点击完按钮界面,按空格就一直触发界面,难搞
- ABP Framework V4.4 RC 新增功能介绍
目录 新增功能概述 启动模板删除 EntityFrameworkCore.DbMigrations 项目 CMS-Kit 动态菜单管理 Razor引擎对文本模板的支持 DbContext/Entiti ...
- 19、oracle的启动和关闭过程
19.1.oracle数据库实例的启动分三步: 1.启动oracle例程: startup nomount; #读初始化参数文件,启动实例,但不安装数据库.当数据库以这个模式启动时,参数文件被读取, ...
- 工作3年,还不会写单元测试?新技能get!
历史遗留代码不敢重构? 每次改代码都要回归所有逻辑? 提测被打回? 在近期的代码重构的过程中,遇到了各式各样的问题.比如调整代码顺序导致bug,取反操作逻辑丢失,参数校验逻辑被误改等. 上线前需要花大 ...
- C++实现二分法详解
二分法是在一个排好序的序列(数组,链表等)中,不断收缩区间来进行目标值查找的一种算法,下面我们就来探究二分法使用的一些细节,以及常用的场景: 寻找一个数: 寻找左侧边界: 寻找右侧边界. 一.二分法的 ...