pat1089. Insert or Merge (25)
1089. 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 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)的更多相关文章
- PAT1089. Insert or Merge
PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...
- 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甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT 1089. Insert or Merge (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089. Insert or Merge (25)
题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...
- 1089. Insert or Merge (25)-判断插入排序还是归并排序
判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...
- PAT (Advanced Level) 1089. Insert or Merge (25)
简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- A1089 Insert or Merge (25 分)
一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
- 09-排序2 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
随机推荐
- git常用命令(转)
git常用命令: git init //初始化本地git环境 git clone XXX//克隆一份代码到本地仓库 git pull //把远程库的代码更新到工作台 git pull --rebase ...
- 「BZOJ 3242」「NOI 2013」快餐店「基环树」
题意 基环树上找到一个点(可以在边上)使得它到树上最远点的距离最小,输出最小距离 题解 如果是一棵树,答案就是树的直径\(/2\) 如果是基环树,那么很好证明删去环上的某一条边是不影响答案的.于是断环 ...
- 循环队列(Joseplus Problem)
#include <iostream> #include <stdio.h> using namespace std; ]; ; void Enqueue(int x) { ) ...
- git 命令总结(转)
结构图: <1> Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新建代码库 # 在当前目录新建 ...
- Linux 下的 etc
/etc etc不是什么缩写,是and so on的意思 来源于 法语的 et cetera 翻译成中文就是 等等 的意思. 至于为什么在/etc下面存放配置文件, 按照原始的UNIX的说法(linu ...
- SQLAlchemy外键的使用
orm可以将数据库存储的数据封装成对象,同时,如果封装的好的话,所有的数据库操作都可以封装到对象中.这样的代码在组织结构上会非常的清晰,并且相对与使用sql语句在sql注入方面会极具降低. SQLAl ...
- CF581C Developing Skills 模拟
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main cha ...
- 本地命令上传文件到服务器以及linux编辑过程中非正常退出问题
一.上传文件到linux服务器首先从你本地切换到你要上传文件的目录,接下来:scp 文件名字 服务器用户名字@服务器ip:存储路径例子:scp index.html root@106.75.229 ...
- winfom实现关闭后一直运行
using PLog; using System; using System.Collections.Generic; using System.Diagnostics; using System.L ...
- appium中driver.wait报IllegalMonitorStateException的解释
在写appium代码的时候,有的人想使用wait方法,写成:driver.wait(),结果抛出异常:IllegalMonitorStateException,看了appium client的api文 ...