PAT_A1089#Insert or Merge
Source:
Description:
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
Keys:
- 插入排序和归并排序
Attention:
- 见注释
Code:
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e3; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,origin[M],sorted[M];
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &origin[i]);
for(int i=; i<n; i++)
scanf("%d", &sorted[i]);
int pos=;
while(pos<n && sorted[pos-]<=sorted[pos]) //可以取等
pos++;
int pt=pos;
while(pos<n && sorted[pos]==origin[pos])
pos++;
if(pos == n)
{
printf("Insertion Sort\n");
while(pt> && sorted[pt-]>sorted[pt])
{
swap(sorted[pt-],sorted[pt]);
pt--;
}
}
else
{
printf("Merge Sort\n");
pos=;pt=;
while(pt)
{
pos *= ;
for(int i=; i<n; i+=pos)
{
for(int j=i+; j<min(n,i+pos); j++)
if(sorted[j-]>sorted[j])
pt=;
}
}
for(int i=; i<n; i+=pos)
sort(sorted+i,sorted+min(i+pos,n));
}
for(int i=; i<n; i++)
printf("%d%c", sorted[i], i==n-?'\n':' '); return ;
}
PAT_A1089#Insert or Merge的更多相关文章
- PTA Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- PAT 1089 Insert or Merge[难]
1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- PAT1089. Insert or Merge
PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...
- pat1089. Insert or Merge (25)
1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...
- 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 ...
- Insert or Merge
7-13 Insert or Merge(25 分) According to Wikipedia: Insertion sort iterates, consuming one input elem ...
随机推荐
- python赞乎--学习开发
- LeetCode 实现 Trie (前缀树)
题目链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree/ 题目大意: 略. 分析: 字典树模板. 代码如下: class Tr ...
- FWT公式一览
总表 真值表 对应运算 FWT IFWT A=B=0 A≠B A=B=1 左项 右项 左项 右项 0 0 1 & L+R R L-R R 0 1 0 ^ L+R L-R (L+R)/2 (L- ...
- 手机网页制作的认识(有关meta标签)(转)
仅用来记录学习: 链接地址:https://blog.csdn.net/ye1992/article/details/22714621
- kubernetes集群的安装异常汇---docker的驱动引擎
异常[kubelet cgroup driver:cgroupfs跟docker cgroup driver:systemd不一致] 异常描述 error: failed to run Kubelet ...
- Js event对象offsetX,pageX,screenX,clientX详解
平时在测量元素位置时难以确定,下面给出具体的event对象中的各种属性,以便日后使用. 检测相对于浏览器的位置:clientX和clientY 当鼠标事件发生时,鼠标相对于浏览器左上 ...
- sqlalchemy防sql注入
银行对安全性要求高,其中包括基本的mysql防注入,因此,记录下相关使用方法: 注意:sqlalchemy自带sql防注入,但是在 execute执行 手写sql时 需要考虑此安全问题 对于 wher ...
- 自动化运维工具ansible简单介绍
ansible架构图 ansible安装(centos7环境下) yum update yum install ansible 验证ansible是否成功安装 ansible --version an ...
- day01 python初识、数据类型、流程控制
今日内容大纲:1,计算机基础. cpu,内存,硬盘,操作系统.2,python的发展与应用.3,python的历史. 2008年python同时更新了两个版本 1,python2x python3x ...
- linux随笔-01
认识linux 开源共享精神 低风险 高品质 低成本 更透明 开源软件的特点 使用自由.修改自由.传播自由.收费自由以及创建衍生品的自由 常见的开源许可协议 GNU GPL(GNU General P ...