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 ;
}

pat1089. Insert or Merge (25)的更多相关文章

  1. PAT1089. Insert or Merge

    PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...

  2. 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 ...

  3. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  4. PAT 1089. Insert or Merge (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  5. 1089. Insert or Merge (25)

    题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...

  6. 1089. Insert or Merge (25)-判断插入排序还是归并排序

    判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...

  7. PAT (Advanced Level) 1089. Insert or Merge (25)

    简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  8. A1089 Insert or Merge (25 分)

    一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...

  9. 09-排序2 Insert or Merge (25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

随机推荐

  1. 具有增删改查功能的表格Demo--【BootStrap】

    http://blog.csdn.net/wangmei4968/article/details/48437175

  2. linux源码安装apache

    apache安装之前,需要安装APR.APR-Util和PCRE依赖包 下载 Apache     下载地址: http://httpd.apache.org/download.cgi   (打开找最 ...

  3. PCA(主成分分析)

    PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维数据的降 ...

  4. [SinGuLaRiTy] NOIP模拟赛(TSY)-Day 2

    [SinGuLaRiTy-2033] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved.                              ...

  5. 从理论认识J2EE

    前言 在学习J2EE这块,看了成套的视频,感觉,感觉,感觉收获不是特别大,没用马老师讲得好,但是多少还是和J2EE打了个招呼,比如J2EE著名的十三个规范,他们有的人说不算什么规范,顶多可以理解为十三 ...

  6. git pull 命令

    作用:取回远程主机某个分支的更新,再与本地的指定分支合并 格式:git pull  <远程主机名> <远程分支名>:<本地分支名> 1. 如果与当前分支合并,则可省 ...

  7. 《图解HTTP》阅读笔记--第四章--HTTP状态码

    第四章.返回结果的HTTP状态码前言:状态码的职责是告诉用户服务器端描述返回的请求,以便用户判断服务器处理是否正常. 状态码由三位数字和原因短语组成,其中三位数字的首位指定了响应类别:---1xx 接 ...

  8. luogu4449 于神之怒加强版(莫比乌斯反演)

    link 给定n,m,k,计算\(\sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)^k\)对1000000007取模的结果 多组数据,T<=2000,1<=N,M,K&l ...

  9. Js 向json对象中添加新元素

    即:var json={a:1,b:2} json.c=3  添加新元素直接使用赋值就行了

  10. javascript的最重要的特性之一:闭包的解决方案

    初始代码: for (var j = 0; j < lnglats.length; j++) { AMap.event.addListener(markers[j], 'mouseover', ...