1089. Insert or Merge (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 resulting 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<cstdio>
#include<cmath>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int line[],order[],line1[];
/*void HeapSort(int *order,int n){
int i=n-1;
while(order[i]>order[(i-1)/2]){
i--;
}
int temp=order[i];
order[i]=order[0];
int j=1;//最小左节点
for(;j<i;j=2*j+1){
if(j+1<i&&order[j+1]>order[j]){
j++;
}
if(order[j]>temp){
order[(j-1)/2]=order[j];
}
}
order[(j-1)/2]=temp;
}*/
void Merge(int order[],int temp[],int l,int r,int e){
int i=l,j=r,p=l;
while(i<r&&j<=e){
if(order[i]<order[j]){
temp[p++]=order[i++];
}
else{
temp[p++]=order[j++];
}
}
while(i<r){
temp[p++]=order[i++];
}
while(j<=e){
temp[p++]=order[j++];
}
}
void Merge_pass(int order[],int temp[],int n,int len){
int i;
for(i=;i<=n-*len;i+=*len){
Merge(order,temp,i,i+len,i+*len-);
}
if(i+len<n){
Merge(order,temp,i,i+len,n-);//Merge(i,i+len,n-1);
}
else{
while(i<n){
temp[i]=order[i];
i++;
}
}
}
void MergeSort(int *line,int *order,int n){
int *temp=new int[n+];
int len=,i;
while(len<n){
Merge_pass(line,temp,n,len);
for(i=;i<n;i++){
line[i]=temp[i];
//cout<<i<<" "<<line[i]<<endl;
}
for(i=;i<n;i++){
if(line[i]!=order[i]){
break;
}
}
if(i==n){
break;
}
len*=;
}
if(len<n){
Merge_pass(temp,order,n,len*);
/*for(i=0;i<n;i++){
cout<<i<<" "<<order[i]<<endl;
}*/
}
delete []temp;
}
int main()
{
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j;
for(i=;i<n;i++){
scanf("%d",&line[i]);
line1[i]=line[i];
}
for(i=;i<n;i++){
scanf("%d",&order[i]);
}
for(i=;i<n;i++){
int temp=line[i];
for(j=i;j>=&&line[j-]>temp;j--){
line[j]=line[j-];
}
line[j]=temp;
for(j=;j<n;j++){
if(line[j]!=order[j]){
break;
}
}
if(j==n){
break;
}
}
if(i==n){//merge sort
printf("Merge Sort\n");
MergeSort(line1,order,n);
}
else{//insertion sort
printf("Insertion Sort\n");
i++;
int temp=order[i];
for(j=i;j>=&&order[j-]>temp;j--){//i一定要指向最后
order[j]=order[j-];
}
order[j]=temp;
}
printf("%d",order[]);
for(i=;i<n;i++){
printf(" %d",order[i]);
}
printf("\n");
return ;
}

最新文章

  1. oracle 12c 加入系统服务
  2. Windows平台下PHP环境搭建
  3. MVC中的时间js格式化
  4. Hadoop学习9--动态增加datanode
  5. 团队作业index
  6. Android源代码编译——编译
  7. TeeChart中Axis的CalcIncrement属性
  8. XP系统VPN设置
  9. poj3613(恰经过N条边的最短路)
  10. python网络爬虫,知识储备,简单爬虫的必知必会,【核心】
  11. 字符串join函数跟+号测试
  12. oledb
  13. zookeeper-监控与优化-《每日五分钟搞定大数据》
  14. Python selectors实现socket并发
  15. javaScript 内置对象-Array数组
  16. java批量向oracle插入数据
  17. x86寄存器总结
  18. Python3基础 os chdir 改变工作目录
  19. scanf()函数分析
  20. hdu 3999 二叉查找树

热门文章

  1. winFrom程序更新自动安装
  2. C#获取WINDOWS系统信息
  3. JetBrains Rider 在 Mac 环境下将 cs 文件生成 exe
  4. MyBatis的使用步骤及配置
  5. 文件上传之form表单篇
  6. ArcGIS api for javascript-图层控制(图层树)
  7. 黑马JavaScript学习一 BOM之Window对象定时器功能
  8. The requested profile &quot;account&quot; could not be activated because it does not exist 无法maven install的 解决办法,勾选红框选择的选项即可
  9. JQuery判断页面是否按下了Enter键
  10. SharePoint开发中可能用到的各种Context(上下文)