题目
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
不难,但很坑的题。
数据量很小,所以不用担心时间开销。
最直接的想法就是直接模拟两种排序的过程,然后依次比较,用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; else ivi=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; }