PAT A 1089. Insert or Merge (25)

来源:互联网 发布:淘宝买的平板刷机 编辑:程序博客网 时间:2024/05/23 22:18

题目

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:

103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort1 2 3 5 7 8 9 4 6 0

Sample Input 2:

103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort1 2 3 8 4 5 7 9 0 6

 

不难,但很坑的题。

数据量很小,所以不用担心时间开销。

最直接的想法就是直接模拟两种排序的过程,然后依次比较,用sort替代真实的插入和合并操作即可。

 

高效些的方法可以从末端向前扫描,找到两个序列不同的地方,比较前面的序列是否完全排序即可分辨是哪种类型的排序。

如果是插入排序,递推一步即可。

如果是合并排序,从头扫描,寻找第一个相互交换位置后可以和原序列相同的子序列,即为已经达到的合并长度,递推一步即可。

 

坑点:

1、插入排序中有插入前后序列不变的情况,需要不断迭代,直到获得不同的序列。

2、交换合并排序和插入排序的判断会出问题,不清楚是代码有问题还是有双解存在……

 

代码:模拟排序

#include <iostream>#include <algorithm>#include <vector>using namespace std;bool isSame(vector<int> &v1,vector<int> &v2);int main(){int n;cin>>n;vector<int> first(n,0),second(n,0),tempv;//第一组,第二个组,临时数据for(int i=0;i<n;i++)//输入cin>>first[i];for(int i=0;i<n;i++)cin>>second[i];vector<int>::iterator ivi;tempv=first;int flag_equ=0;//是否相等标志for(int i=0;i<n;i++)//判断插入排序{sort(tempv.begin(),tempv.begin()+i+1);//不断插入,对比if(isSame(tempv,second)){for(;i<n;i++)//!!!要找一个结果改变的项,大坑货{ivi=tempv.begin()+min(n,i+2);sort(tempv.begin(),ivi);if(!isSame(tempv,second))//结果改变,输出{cout<<"Insertion Sort\n";cout<<tempv[0];for(int j=1;j<n;j++)cout<<" "<<tempv[j];return 0;}}}}tempv=first;int len=1,begin,pos;//合并的长度flag_equ=0;while(len<=n)//合并排序{if(isSame(tempv,second))flag_equ=1;len*=2;begin=0;while(begin<n)//合并{if(begin+len<n)ivi=tempv.begin()+begin+len;elseivi=tempv.end();sort(tempv.begin()+begin,ivi);begin+=len;}if(flag_equ==1&&!isSame(tempv,second))//找到一个改变结果的合并{cout<<"Merge Sort\n";cout<<tempv[0];for(int i=1;i<n;i++)cout<<" "<<tempv[i];return 0;}}return 0;}bool isSame(vector<int> &v1,vector<int> &v2){for(int i=0;i<v1.size();i++)if(v1[i]!=v2[i])return false;return true;}


 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微博永久性封号怎么办 电脑占用内存多怎么办 直播签约后悔了怎么办 移动宽带网络不稳定怎么办 股票帐号被锁定怎么办 电脑帐号锁定了怎么办 云校家帐号被锁定怎么办 qq音乐停止运行怎么办 做事效率低怎么办教案 孩子做事效率低怎么办 百度搜不到答案怎么办 学乐云账号忘了怎么办 wps云空间不足怎么办 超星密码忘了怎么办 百度钱包忘记密码怎么办 宝宝吃了粉笔怎么办 粉笔灰进眼睛怎么办 讲公开课紧张怎么办 课堂派考勤旷课怎么办 2018qq音乐付费怎么办 不满一个月社保怎么办 试用期未买社保怎么办 小视频转发黑屏怎么办 听歌要钱的怎么办 手机歌曲要付费怎么办 安装包已损坏怎么办 方舟 安装包损坏怎么办 安装包已删除怎么办 超大附件过期了怎么办 邮箱被黑客盯上怎么办 云闪付安全问题忘记了怎么办 不知道网易账号怎么办 登录过程初始化失败怎么办 登录进程初始化失败怎么办 网易邮箱地址忘了怎么办 电脑电源短路了怎么办 电脑电源有问题怎么办 手机账号忘了怎么办 简历周五发了怎么办 网易邮箱被注销怎么办 foxmail接收密码忘记怎么办