Source:

PAT A1089 Insert or Merge (25 分)

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的更多相关文章

  1. PTA Insert or Merge

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

  2. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  3. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  4. PAT甲级1089. Insert or Merge

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

  5. PAT 1089 Insert or Merge[难]

    1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  6. PAT1089. Insert or Merge

    PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...

  7. pat1089. Insert or Merge (25)

    1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...

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

  9. Insert or Merge

    7-13 Insert or Merge(25 分) According to Wikipedia: Insertion sort iterates, consuming one input elem ...

随机推荐

  1. linux下载文件到本地命令

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/mengda_lei/article/de ...

  2. upc组队赛15 Lattice's basics in digital electronics【模拟】

    Lattice's basics in digital electronics 题目链接 题目描述 LATTICE is learning Digital Electronic Technology. ...

  3. spring data jpa 使用方法命名规则查询

    按照Spring Data JPA 定义的规则,查询方法以findBy开头,涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性首字母需大写.框架在进行方法名解析时,会先把方法名多余的前缀 ...

  4. luoguP1514 引水入城 题解(NOIP2010)(Bfs+贪心)

    P1514 引水入城  题目 #include<iostream> #include<cstdlib> #include<cstdio> #include<c ...

  5. CTU Open 2018 Lighting /// 组合数递推 二进制

    题目大意: 给定n k 给定一个数的二进制位a[] 求这个数加上 另一个二进制位<=n的数b 后 能得到多少个不同的 二进制位有k个1 的数 样例 input10 51000100111 out ...

  6. Oracle update 两表及以上关联更新,出现多值情况,不是一对一更新

    为了方便起见,建立了以下简单模型,和构造了部分测试数据:在某个业务受理子系统BSS中, SQL 代码--客户资料表 create table customers ( customer_id numbe ...

  7. 十一、结构模式之享元(Flyweight)模式

    什么是享元模式 享元模式是对象的结构模式,是运用共享技术来有效的支持大量细粒度的对象.享元对象能做到共享的关键是区分内蕴状态和外蕴状态.一个内蕴状态是存储在享元对象内部,并且是不会随环境改变而有所不同 ...

  8. CSS中强大的EM(转)

    转自:https://www.w3cplus.com/css/px-to-em CSS中强大的EM 作者:大漠 日期:2011-10-27 点击:97370 em 长度单位 编辑推荐:3月31日前,点 ...

  9. postgres之清理空间碎片

    postgres=# select * from pg_stat_user_tables where relname = 'test'; -[ RECORD 1 ]-------+---------- ...

  10. Cytoscape——实例

    本文将具体操作怎样用Cytoscape绘制网络图 Cytoscape所支持的数据格式:1.*.sif格式:  nodeA<interaction>nodeB  nodeC<inter ...