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;}
- PAT A 1089. Insert or Merge (25)
- PAT(A) - 1089. Insert or Merge (25)
- PAT-A 1089. Insert or Merge (25)
- PAT-A-1089. Insert or Merge (25)
- Pat(A) 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25)
- PAT(Advance) 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25)
- 【PAT】1089. Insert or Merge (25)
- PAT 甲级 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge
- 【PAT】1089. Insert or Merge
- PAT 1088 1089. Insert or Merge (25)(排序啊)
- 1089. Insert or Merge (25)-PAT甲级真题
- [转]PAT甲级练习1089. Insert or Merge (25)
- 【PAT】【Advanced Level】1089. Insert or Merge (25)
- PAT 1089. Insert or Merge (25) 允许有重复元素!!!!!!!!!!!!!
- 正则表达式中 圆点"."的应用
- 5天学会jaxws-webservice编程第一天
- cas3.5.2配置数据源(mysql为例)
- Jquery实现checkbox的全选,全不选,反选,提交
- UVAOJ 409 ——Excuses, Excuses! 模拟
- PAT A 1089. Insert or Merge (25)
- java string 创建对象
- 30岁,辞掉年薪20万的工作开始创业,目前月薪2k
- Android高手进阶教程(六)之----Android 中MenuInflater的使用(布局定义菜单)!
- 自定义HttpClient工具类
- 阿里云服务器最新版Cent 70系统上LAMP环境的配置
- Win7 便签设置字体方法
- 面向对象的特征
- 报表软件公司高价悬赏BUG,100块1个我真是醉了