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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- Deep Dive into Neo4j 3.5 Full Text Search
In this blog we will go over the Full Text Search capabilities available in the latest major release ...
- 精选15个国外CSS框架
转自:http://blog.bingo929.com/css-frameworks-15.html 什么是css框架 实际上还是让我们从框架说起吧.框架就是一个你可以用于你的网站项目的基本的概念上的 ...
- JDBC_Template(简化代码)
/** * @Description: TODO(这里用一句话描述这个类的作用) * @Author aikang * @Date 2019/8/27 11:03 */ /* Spring JDBC: ...
- sql 递归查询,刁刁的
with cte as( select IDPlus,SuperiorsIDPlus,RoleGrade,viplevel,NAME,WeixinId from Member where IDPlus ...
- 获取的Json数据需要去掉特殊符号
我们平时在后台代码里面获取的Json数据如下情况: json会包含\r ,\n, \ 等一些特殊的符号.下面我们就写一个方法去消除这些特殊符号,在代码里获取到纯净的json. public stat ...
- CSIC_716_20191114【生成器、匿名函数、内置函数、三元表达式、列表生成式、生成器表达式】
生成器: 函数与yield连用,凡是函数中有yield的,调用该函数的时候均不会立即执行,而是会返回一个生成器. 生成器本质上是一个迭代器,需要通过 [生成器.__next__()]或者[nex ...
- css---动画封装
animation-name 属性指定应用的一系列动画,每个名称代表一个由@keyframes定义的动画序列 值: none 特殊关键字,表示无关键帧. keyframename 标识动画的字符串 a ...
- COGS2355 【HZOI2015】 有标号的DAG计数 II
题面 题目描述 给定一正整数n,对n个点有标号的有向无环图(可以不连通)进行计数,输出答案mod 998244353的结果 输入格式 一个正整数n 输出格式 一个数,表示答案 样例输入 3 样例输出 ...
- soapui打开即报错------连接不上Internet
1.遇到的问题: 打开soapui即报错,如下: You're getting this message since your computer is offline and SoapUI can't ...
- Windows 子网掩码
子网掩码(subnet mask)又叫网络掩码.地址掩码.子网络遮罩,它是一种用来指明一个IP地址的哪些位标识的是主机所在的子网,以及哪些位标识的是主机的位掩码.子网掩码不能单独存在,它必须结合IP地 ...