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. 英文操作系统中中文乱码(SQL中 NVARCHAR 和 VARCHAR区别)

        varchar在SQL Server中是采用单字节来存储数据的,nvarchar是使用Unico来存储数据的.中文字符存储到SQL Server中会保存为两个字节(一般采用Unico编码),英 ...

  2. 扫描线-小Z的桌子

    大概题意:在一个01矩阵中找到一个周长最大的全0矩形. 这道题用的是扫描线,O(n^2),求最大面积的思路完全可以放在这里.下面说说思路. 首先,一个最大周长子矩形(最大周长全0矩形),左右两侧的列上 ...

  3. django 基础框架学习 (三)

    Django框架基础-03数据库新增数据    1.save⽅法        >>> from datetime import date        >>> f ...

  4. vue2.0 vs vue1.0

    1.每个组件模板不支持代码片段组件中模板之前<template> <h3>as</h3></template>现在 必须要有根元素 包裹住所有代码< ...

  5. 如何实现 MySQL 的读写分离?MySQL 主从复制原理的是啥?如何解决 MySQL 主从同步的延时问题?

    如何实现 MySQL 的读写分离? 其实很简单,就是基于主从复制架构,简单来说,就搞一个主库,挂多个从库,然后我们就单单只是写主库,然后主库会自动把数据给同步到从库上去. MySQL 主从复制原理的是 ...

  6. max Count Group by

    select UserName ,Max(LoginTime),count(1) from LoginLog group by UserName 只 group by UserName就行 可以得到L ...

  7. C语言预处理命令之文件包含

    文件包含预处理命令的一般形式是: #include<文件名> 或者 #include“文件名” #include命令告诉预处理器用指定文件的内容替换这条命令,两种不同的命令格式决定了预处理 ...

  8. Github命令git status

    输入git status可以告诉我们三件事: 1.你当前的本地库位于哪个分支上: 2.你当前的本地库和远程库的区别:它会提示本地库和远程库差了还是多了多少个提交(commit),并给你提建议,要不要p ...

  9. BestCoder Round #64 1001

    Numbers  Accepts: 480  Submissions: 1518  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6553 ...

  10. (转)Caffe搭建:常见问题解决办法和ubuntu使用中遇到问题(持续更新)

    参考网址:http://www.cnblogs.com/empty16/p/4828476.html 严正声明: 在linux下面使用命令行操作时,一定要懂得命令行的意思,然后再执行,要不然在不知道接 ...