PAT甲级——A1089 Insert or Merge
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 (≤). 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来代替排序的部分,无论是合并排序还是插入排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int N, a;
vector<int>nums, ans, temp;
int main()
{
cin >> N;
for (int i = ; i < N; ++i)
{
cin >> a;
nums.push_back(a);
}
for (int i = ; i < N; ++i)
{
cin >> a;
ans.push_back(a);
}
temp = nums;
bool f = false;
for (int i = ; i < N; ++i)//进行插入排序
{
sort(temp.begin(), temp.begin() + i + );//第一趟排序应该是排序前两个数,第i趟排序分别排序前i+1个数
if (temp == ans)
{
f = true;
cout << "Insertion Sort" << endl;
sort(temp.begin(), temp.begin() + i + );//再一次迭代
break;
}
}
if (f == false)//那就是合并排序
{
cout << "Merge Sort" << endl;
temp = nums;
for (int i = ; i < N; i *= )//将每i个元素归并为一个非递减序列
{
for (int j = ; j < N; j += i)
sort(temp.begin() + j, temp.begin() + (j + i < N ? j + i : N));
if (temp == ans)
{
for (int j = ; j < N; j += i * )//进行下一次迭代
sort(temp.begin() + j, temp.begin() + (j + i * < N ? j + i * : N));
break;
}
}
}
for (int i = ; i < N; ++i)
cout << temp[i] << (i < N - ? " " : "");
return ;
}
PAT甲级——A1089 Insert or Merge的更多相关文章
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- A1089. Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- A1089 Insert or Merge (25 分)
一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
- PAT Advanced 1089 Insert or Merge (25) [two pointers]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- PAT_A1089#Insert or Merge
Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT 1089 Insert or Merge[难]
1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- Codeforces 479【B】div3
题目链接: http://codeforces.com/problemset/problem/977/B 题意:字符串,找固定长度为2的重复子串出现次数最多的. 题解:我暴力做的.暴力出奇迹. #in ...
- 2018湘潭大学程序设计竞赛【D】
题目链接:https://www.nowcoder.com/acm/contest/105/D 题意:就是数的fib表示方法.按权展开,又按二进制算出结果输出. 题解:贪心和数论吧.找到跟数最接近的f ...
- 2018-12-25-C#-7.2-通过-in-和-readonly-struct-减少方法值复制提高性能
title author date CreateTime categories C# 7.2 通过 in 和 readonly struct 减少方法值复制提高性能 lindexi 2018-12-2 ...
- JS对象 Date 日期对象 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒)。 定义一个时间对象 : var Udate=new Date();Date()的首字母须大写
Date 日期对象 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义一个时间对象 : var Udate=new Date(); 注意:使用关键字new,Date()的首 ...
- .net 接受请求过来的流
//接收POST过来的数据 System.IO.Stream s = Request.InputStream; int count = 0; byte[] buffer = new byte[1024 ...
- windows下 Mysql 8.0.x 数据库简单的导出和导入!!!
1.首先需要进入到mysql安装目录下的bin目录,执行cmd进入命令窗口. 2.导出(导出某个数据库,也可以针对某张表导出)2.1导出数据结构以及数据的命令: mysqldump -u root - ...
- MySQL基本命令脚本
一.基本命令 1.启动服务 说明:以管理员身份运行cmd 格式:net start 服务名称 示例:net start mysql57 2.停止服务 说明:以管理员身份运行cmd 格式:net sto ...
- SDOI2018
SD的题有点反人类啊... d1t1[SDOI2018]物理实验 感觉比较好想但不太好写,写了一半弃了 d1t2[SDOI2018]战略游戏 建出圆方树,每次建虚树,答案就是虚树上的原点个数减去询问的 ...
- 运维人员最常用150个linux命令汇总
命令 功能说明 线上查询及帮助命令(2个) man 查看命令帮助,命令的词典,更复杂的还有info,但不常用. help 查看Linux内置命令的帮助,比如cd命令. 文件和目录操作命令(18个) l ...
- 第十二篇:随手记一下javaBean的setter,getter方法的命名问题
今天测试新写的一个系统表的完整Ibatis配置和调用,因为经验少,到处撞墙...其他的坑爹问题就不提了 测试sqlMapClient.queryForList("...",para ...