Insert or Merge
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的更多相关文章
- 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 ...
- PAT_A1089#Insert or Merge
Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...
随机推荐
- github最值得收藏的Bootstrap3后台管理框架
1. AdminLTE AdminLTE是一个完全响应的后台管理模板.基于Bootstrap3框架.高度可定制,易于使用.适合许多屏幕分辨率从小型移动设备到大型台式机. GitHub AdminLTE ...
- HDU 6183 Color it(动态开点线段树)
题目原网址:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题目中文翻译: Time Limit: 20000/10000 MS (Java/Others ...
- websocket实现群聊
server # @File: 群聊 from flask import Flask, render_template, request from geventwebsocket.handler im ...
- Python读取文件行数不对
对于一个大文件,读取每一个行然后处理,用readline()方法老是读不全,会读到一半就结束,也不报错: 总之处理的行数跟 wc -l 统计的不一样,调试了一下午,改用 with open('xxx. ...
- Lumia 刷机(强刷)Message send failed解决办法
强刷可以救砖,不需要验证地区code,可以跨刷其它国家/地区的固件,但不是所有机型都可以这样,Lumia 620是支持跨刷的. 看本文你首先要知道使用Nokia Care Suite强刷的步骤,参考从 ...
- Java-每日编程练习题③
一.计算圆周率 中国古代数学家研究出了计算圆周率最简单的办法: PI=4/1-4/3+4/5-4/7+4/9-4/11+4/13-4/15+4/17...... 这个算式的结果会无限接近于圆周率的值, ...
- 【经验总结】关于使用某些第三方插件库元素设置display:none后重新show不显示的问题;(display、opacity、宽高0的使用场景)
display:none 直接取消元素所占用的位置(但是元素还是存在的),后面元素看他就相当于不存在了: opacity:0 隐藏,但是其依旧占用位置: height.width:0 和displa ...
- 【js】js导出表格到excel
js: function method(tableid) //读取表格中每个单元到EXCEL中 { var curTbl = document.getElementById(tableid); var ...
- Android iconfont字体图标的使用
1.首先,进入阿里的矢量图标库,在这个图标库里面可以找到很多图片资源,当然了需要登录才能下载或者使用,用GitHub账号或者新浪微博账号登录都可以 2.登录以后,可以搜索自己需要的资源,然后直接下载使 ...
- iOS 音频视频制作
--iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...