7-13 Insert or Merge(25 分)

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 (≤100). 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
 #include<iostream>
using namespace std;
void Merge(int sequence1[],int l,int r,int rend,int temp[]){
int lend=r-;
int s=l;
while(l<=lend&&r<=rend){
if(sequence1[l]<sequence1[r]) temp[s++]=sequence1[l++];
else temp[s++]=sequence1[r++];
}
while(l<=lend) temp[s++]=sequence1[l++];
while(r<=rend) temp[s++]=sequence1[r++];
}
int main(){
int N;
int flag=;//Insertion_sort
cin>>N;
int sequence[N];
int sequence1[N];
for(int i=;i<N;i++)
cin>>sequence[i];
for(int i=;i<N;i++)
cin>>sequence1[i];
int i,j;
for(j=;j<N-;j++)
if(sequence1[j]>sequence1[j+]) break;
int sign=j;
for(j=j+;j<N;j++)
if(sequence[j]!=sequence1[j]) flag=;//Merge_sort
if(flag==){
cout<<"Insertion Sort"<<endl;
int temp=sequence1[sign+];
for(i=sign+;i>=;i--)
if(temp<sequence1[i-])
sequence1[i]=sequence1[i-];
else break;
sequence1[i]=temp;
int tag=;
for( i=;i<N;i++){
if(tag++==) cout<<sequence1[i];
else cout<<" "<<sequence1[i];
}
}
else{
int tag=;
cout<<"Merge Sort"<<endl;
int length;
for(length=;length<=N;length*=){
for(i=length-;i<N-;i+=*length)
if(sequence1[i]>sequence1[i+])
{tag=; break;}
if(tag==) break;
}
int temp[N];
for(i=;i<N-*length;i+=*length)
Merge(sequence1,i,i+length,i+*length-,temp);
if(i+length<N)
Merge(sequence1,i,i+length,N-,temp);
else
for(;i<N;i++) temp[i]=sequence[i];
int tick=;
for(int k=;k<N;k++)
if(tick++==) cout<<temp[k];
else cout<<" "<<temp[k];
cout<<endl;
}
return ;
}
 

 

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. PAT_A1089#Insert or Merge

    Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...

随机推荐

  1. JSP | 基础 | 两种方式循环输出

    用循环连续输出三个你好,字体从小变大 第一种: <body> <%! //通过表达式方式调用实现 String HelloJSP1(){ String str = "&qu ...

  2. 2017年“嘉杰信息杯” 中国大学生程序设计竞赛全国邀请赛 Highway

    Highway Accepted : 122   Submit : 393 Time Limit : 4000 MS   Memory Limit : 65536 KB Highway In ICPC ...

  3. 洛谷 P3327 [SDOI2015]约数个数和 || Number Challenge Codeforces - 235E

    https://www.luogu.org/problemnew/show/P3327 不会做. 去搜题解...为什么题解都用了一个奇怪的公式?太奇怪了啊... 公式是这样的: $d(xy)=\sum ...

  4. 用eclipse-inst-win64.exe安装eclipse出现Java for Windows Missing 的原因

    Java for Windows Missing 因为jdk的版本没有对,我这里是64位的机器上安了32位的jdk,所以一直报这个. 必须换上相对应版本的jdk,提示页面有链接,直接点击就可以下载. ...

  5. GCC KEIL ARM编译器

    经常用keil,也听说IAR的编译效率很高,原来C51时用proteus,最近proteus8开始支持stm32,所以在研究用keil5+HAL+proteus学习STM32F. 问题:因为prote ...

  6. HDU 1223 打表 + 大数

    http://acm.hdu.edu.cn/showproblem.php?pid=1223 一般遇到这些题,我都是暴力输出前几项,找规律.未果. 然后输出n = 1时候,以A开始,有多少个答案, n ...

  7. zoj3772Calculate the Function(矩阵+线段树)

    链接 表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护 对于每一个点够一个2*2的矩阵 1 a[i] 1  0   这个矩阵应该不陌生 类似于构造斐波那契的那个数列 ...

  8. qconbeijing2016

    http://2016.qconbeijing.com/schedule 大会日程 2016年04月21日 星期四 09:15 开场致辞 地点 1号厅   主题演讲 工程效率提升 业务核心架构 容器集 ...

  9. Git之删除本地和远程项目

    目录 删除本地项目 删除远程项目 删除本地项目: git rm -rf project  或者 rm -rf project   [删除工作区项目] git add project [将删除的项目添加 ...

  10. iOS 使用UIBezierPath和CAShapeLayer画各种图形

    CAShapeLayer 是 CALayer 的子类,但是比 CALayer 更灵活,可以画出各种图形,当然,你也可以使用其他方式来画,随你. 杂谈 在 CAShapeLayer 中,也可以像 CAL ...