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数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
随机推荐
- php下载
生成迅雷下载链接 $url = "http://www.xxx.com/xxx/test.jpg"; echo "thunder://".base64_enco ...
- JS-ValidForm:介绍
ylbtech-JS-ValidForm:介绍 1.返回顶部 1. 关于Validform Validform:一行代码搞定整站的表单验证! 1 $(".demoform").Va ...
- 77、tensorflow手写识别基础版本
''' Created on 2017年4月20日 @author: weizhen ''' #手写识别 from tensorflow.examples.tutorials.mnist import ...
- html5中的拖拽功能
拖拽元素支持的事件 ondrag 应用于拖拽元素,整个拖拽过程都会调用 ondragstart 应用于拖拽元素,当拖拽开始时调用 ondragleave 应用于拖拽元素,当鼠标离开拖拽元素是调用 on ...
- 通过一条很慢的SQL梳理下SQL优化基础
- python获取沪股通、深股通、港股通每日资金流向数据
接口:moneyflow_hsgt 描述:获取沪股通.深股通.港股通每日资金流向数据,每次最多返回300条记录,总量不限制. 注:tushare模块下载和安装教程,请查阅我之前的文章 输入参数 名称 ...
- Linux的各个发行版本(一)
三大流派 1.Slackware SUSE Linux Enterprise Server (SLES) OpenSuse桌面 2.debian 迄今为止最遵循GNU规范的Linux系统 Ubuntu ...
- 【目录】Asp.NETCore轻松学系列
随笔分类 - Asp.NETCore轻松学系列 Asp.NETCore轻松学系列阅读指引目录 摘要: 耗时两个多月,坚持写这个入门系列文章,就是想给后来者更好更快的上手体验,这个系列可以说是从入门到进 ...
- centos6.5大于2T容量硬盘分区、格式化、挂载!
一. 超过2T容量硬盘使用parted创建分区 1.fdisk l(查看需要分区的硬盘盘符如:sda) 2.parted /dev/sda 3.rm 1 4.mklabel gpt(超过2T磁盘格式 ...
- 【sql】牛客网练习题 (共 61 题)
[1]查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` ) NOT NULL, `birth_date` date NOT NULL, `first_ ...