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. C++ typedef typename 作用

    C++ typedef typename 作用 C++的一些语法让人看着费解,其中就有: typedef typename std::vector<T>::size_type size_t ...

  2. html 文本溢出显示省略号 .....

  3. eclipse 当安装jad仍然不能反编译,提示attach source的时候

    当安装jad仍然不能反编译,提示attach source的时候,其实是当前workspace有问题了: 所使用的workspace目录下.metadata\.mylyn会出现一个.tasks.xml ...

  4. 日历 php

    <?php $year=@$_GET['year']; //获得地址栏的年份 $month=@$_GET['month']; //获得地址栏的月份 if(empty($year)) $year= ...

  5. 500 Keyboard Row 键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 详见:https://leetcode.com/problems/keyboard-row/description/ C++: cl ...

  6. A. The Fault in Our Cubes 暴力dfs

    http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行, ...

  7. Suricata产生的数据存储目录

    不多说,直接上干货! 我这里呢,分两种常用的Suricata. 一.源码编译安装的Suricata 这里不多说,大家可以去看我下面写的博客 使用 Suricata 进行入侵监控(一个简单小例子访问百度 ...

  8. 记一次有关spark动态资源分配和消息总线的爬坑经历

    问题: 线上的spark thriftserver运行一段时间以后,ui的executor页面上显示大量的active task,但是从job页面看,并没有任务在跑.此外,由于在yarn mode下, ...

  9. PKU_campus_2018_H Safe Upper Bound

    思路: 题目链接http://poj.openjudge.cn/practice/C18H/ 用2147483647除以最大素因子. 这里用了Pollard_rho因子分解算法,模板参考了http:/ ...

  10. Web前端攻防,一不小心就中招了

    随着各浏览器安全功能的提高,前端防御面临的问题也没有之前那么复杂,但浏览器的防御措施并不能百分百的保证网站的安全. 浏览器的XSS Auditor,使得反射型xss几乎被废:CSP(Content-S ...