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 ...
随机推荐
- HTML与CSS入门经典(第9版)试读 附随书源码 pdf扫描版
HTML与CSS入门经典(第9版)是经典畅销图书<HTML与CSS入门经典>的最新版本,与过去的版本相同,本书采用直观.循序渐进的方法,为读者讲解使用HTML5与CSS3设计.创建并维护世 ...
- 使用sphinx快速生成Python API 文档
一 简单介绍 不管是开源还是闭源,文档都是很重要的.当然理论上说,最好的文档就是代码本身,但是要让所有人都能读懂你的代码这太难了.所以我们要写文档.大部分情况,我们不希望维护一份代码再加上一份文档, ...
- python logging日志库
项目中使用的日志库是使用python官方库logging封装的,但是居然一直么有设置日志自动滚动,经常会受到告警说哪台机器磁盘空间又满,清理一下,于是研究一下,解决这个问题. 参考:https://d ...
- JavaScript之DOM实践
前言 HTML的DOM是JavaScript有能力对HTML作出反应,有时候,我们需要一些网页效果,为了更好地适应用户的效果,比如,我们平时接触,点击某个按钮,按下去的瞬间会变色,再比如,有时候鼠标经 ...
- 【三支火把】--- shell脚本中变量的类型及作用域
一直对shell脚本有一种特殊的感觉,因此花了一段时间学习,本人擅长C语言编程,深受C语言荼毒,在学习其他任何类似于编程语言的东东的时候,都会不自觉的与C进行对比,因此对于shell中的变量的作用域一 ...
- Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) B 1075B (思维)
B. Taxi drivers and Lyft time limit per test 1 second memory limit per test 256 megabytes input stan ...
- C语言讲解命令行参数
命令行(command line):是在命令行环境中,用户为运行程序输入命令的行. 命令行参数(command-line argument): 是同一行的附加项. C编译器允许main()没有参数或者 ...
- 在Ubuntu中使用AppImage类型文件
右键--属性---权限---允许作为执行文件启动
- P2421 [NOI2002]荒岛野人
传送门 答案不大于 $10^6$,考虑枚举答案 对于枚举的 ans,必须满足对于任意 i,j(i≠j) 都有 使式子$c_i+kp_i \equiv c_j+kp_j\ (mod\ ans)$成立的最 ...
- 微信小程序填坑之旅
一.解决scroll-view的滚动条问题 1.出现场景css. 有些场景下scroll-view是不需要滚动条的,比如顶部导航栏的横向滑动.而在单页的css样式中加入特定代码不能影响到全局的样式 2 ...