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 resuling 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<bits/stdc++.h>
using namespace std; const int maxn=; int a[maxn],b[maxn],c[maxn]; int n; bool judge(int*a,int *b)
{
for(int i=;i<n;i++)
{
if(a[i]!=b[i])
return false;
} return true;
} void print(int* a){
for(int i=;i<n;i++){ if(i>)
cout<<" "; cout<<a[i];
}
cout<<endl;
} bool InsertSort(){ bool flag=false; for(int i=;i<n;i++){ int j=i; int temp=b[j]; if(i!=&&judge(b,c)){
flag=true;
} while(j>&&temp<b[j-])//这里错把temp写成b[j]了,要细心
{
b[j]=b[j-];
j--;
} b[j]=temp; if(flag){ return true;
} } return false; } void MergeSort(){ bool flag=false; for(int step=;step/<=n;step*=){ if(step!=&&judge(b,c)){
flag=true;
} for(int i=;i<n;i+=step){ sort(b+i,b+min(i+step,n));
} if(flag){
cout<<"Merge Sort\n";
print(b);
return;
} } } int main(){
cin>>n; for(int i=;i<n;i++){
cin>>a[i];
b[i]=a[i];
} for(int i=;i<n;i++){
cin>>c[i];
} if(InsertSort()){ cout<<"Insertion Sort\n"; print(b);
}else{ for(int i=;i<n;i++)
b[i]=a[i]; MergeSort();
} return ;
}
1089 Insert or Merge (25 分)的更多相关文章
- 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 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 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)
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数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
随机推荐
- flex 布局 实现三点筛子
实现麻将中三点筛子:效果如下图 具体实现代码: html代码: <div class="box"> <div class="item"> ...
- 个人笔记 - C++相关收藏
一.文件操作 1.C++从txt文件中读取二维的数组
- idea Maven 一键 mvn clean package
文章目录 方法一 方法二 方法一 方法二
- 泛微oa系统com.eweaver.base.DataAction文件sql参数sql注入
URL/ServiceAction/com.eweaver.base.DataAction?sql=select%201,2,3,4,5,6,7,8,9,233%20from%20DUAL%20
- 洛谷P1629 邮递员送信 最短路-Djistra
先上一波题目qwq https://www.luogu.org/problem/P1629· 复习了一波 dijstra 的 priority_queue(优先队列)优化的写法 tips: 求单项路中 ...
- 为什么javaBean要有get/set方法的设计
可以应对将来的修改,比如有一个以长度计算的项目开发好了,过段时间客户说这个项目要在美国上市,有了javaBean只要把所有以厘米计算的单位都乘以2.54转化为正确单位交给客户 public class ...
- nginx中root与alias关键字的区别
前言 近段时间秋招上岸了,于是每天疯狂补各种分布式基础,每天都在痛苦与快乐中度过. 在学习 nginx 的时候,遇到配置上的问题:root 与 alias 的区别,卡了大概三个小时,记录下来警醒自己不 ...
- Linux 版本查詢
# uname -a 查看 Kernel版本 # cat /etc/redhat-release查看 linux版本(以RedHat為例) 1.核心查詢:uname -a結果:Linux 2.x.x ...
- Could not autowire. No beans of 'int' type found. less... (Ctrl+F1) Checks autowiring problems in a bean class.
package com.cxy.netty.controller; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel ...
- 在Ubuntu下安装source Insight
在ubuntu中,安装windows程序用wine,然后用wine安装windows软件即可. 安装wine sudo apt-get install wine 下载sourceinsight的exe ...