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. docker安装部署命令

    一.安装工具包 $ sudo yum install -y yum-utils #安装工具包,缺少这些依赖将无法完成 二.设置远程仓库 $sudo yum-config-manager --add-r ...

  2. mootools使用Request.send()数据时刷新整个页面

    今天使用mootools做ajax登录时使用Request().send()数据时老是刷新整个页面,换成Requesr.JSON()也不行,弄了好长时间,很郁闷啊,最后终于发现onSuccess:fu ...

  3. LightOJ 1418 Trees on My Island (Pick定理)

    题目链接:LightOJ 1418 Problem Description I have bought an island where I want to plant trees in rows an ...

  4. 2644. 数列 (Standard IO)

    这道题是道数论题,如果想对了的话会很快. 因为这道题实在是没有什么知识点,所以我直接上代码,代码上有很详细的注释: #include<iostream> #include<cstdi ...

  5. tar.xz 解压

    解压tar.xz文件:先 xz -d xxx.tar.xz 将 xxx.tar.xz解压成 xxx.tar 然后,再用 tar xvf xxx.tar来解包. xz -d Python-3.7.1.t ...

  6. 第二节 RabbitMQ配置

    原文:第二节 RabbitMQ配置 版权声明:未经本人同意,不得转载该文章,谢谢 https://blog.csdn.net/phocus1/article/details/87281553 1.配置 ...

  7. Groovy学习:第一章 用Groovy简化Java代码

    1. Groovy的安装 目前Groovy的最新版本为2.1.2版,下载地址为:http://groovy.codehaus.org/Download下载后解压groovy-binary-2.1.2. ...

  8. go语言从例子开始之Example17.指针

    Go 支持 指针,允许在程序中通过引用传递值或者数据结构 Example: package main import "fmt" func zeroval(ival int){ iv ...

  9. 联想think system sr550信息

    带外管理口 默认IP地址:192.168.70.125 默认用户名密码 USERID PASSW0RD    0是数字0

  10. python-django_rest_framework中的request/Response

    rest_framework中的request是被rest_framework再次封装过的,并在原request上添加了许多别的属性: (原Django中的request可用request._requ ...