1089. Insert or Merge
来源:互联网 发布:淘宝网商城女童鞋 编辑:程序博客网 时间:2024/05/17 08:14
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 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 0Sample Output 1:
Insertion Sort1 2 3 5 7 8 9 4 6 0Sample Input 2:
103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6Sample Output 2:
Merge Sort1 2 3 8 4 5 7 9 0 6
注意一下下标对齐和数组溢出,可能是我写法不标准,开始的时候会有越界,细节的地方修改一下。
其实归并的时候找好下标直接sort也可以,我自己写了下,尾部余下的部分用的sort。
#include<iostream>#include<algorithm>#include<vector>using namespace std;int main(){int n;cin>>n;vector<int> a(n),b(n),ans(n),tmp(n);for(int i=0;i<n;++i)cin>>a[i];for(int i=0;i<n;++i)cin>>b[i];int idx=0;if(n>1){idx++;while(idx<n&&b[idx-1]<=b[idx])idx++;}bool f=true;for(int i=idx;i<n;++i)if(a[i]!=b[i]){f=false;break;}if(f){ans=b;if(idx<n)sort(ans.begin(),ans.begin()+idx+1);cout<<"Insertion Sort"<<endl;}else{cout<<"Merge Sort"<<endl;ans=a;tmp=a;int l=1;while(!f&l<=n/2){if(ans==b)f=true;for(int i=0;i+2*l-1<n;i=i+2*l){int cnt1=0,cnt2=0;while(cnt1<l&&cnt2<l){if(ans[i+cnt1]<ans[i+l+cnt2]){tmp[i+cnt1+cnt2]=ans[i+cnt1];cnt1++;}else{tmp[i+cnt1+cnt2]=ans[i+l+cnt2];cnt2++;}}while(cnt1<l){tmp[i+cnt1+l]=ans[i+cnt1];cnt1++;}while(cnt2<l){tmp[i+cnt2+l]=ans[i+l+cnt2];cnt2++;}}if(n%(2*l))sort(tmp.end()-n%(2*l),tmp.end());ans=tmp;l=l*2;}}cout<<ans[0];for(int i=1;i<n;++i)cout<<" "<<ans[i];return 0;}
- 1089.Insert or Merge
- 1089. Insert or Merge
- 1089. Insert or Merge
- 1089. Insert or Merge
- 1089. Insert or Merge
- 1089. Insert or Merge (25)
- PAT 1089. Insert or Merge
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- C++实现打印一年中所有的星期三
- JavaScript学习笔记(5)
- js中的设计模式
- Java并发基础(八)-locks包
- Visual Studio 2010 IDE 使用之美 之一
- 1089. Insert or Merge
- js跨域问题 常见的集中解决方案
- HDU 1829 A Bug's Life(并查集)
- Visual Studio 2010 实用功能总结
- Java数组-查表法-进制转换
- Tomcat是如何将JSP代码编译成Servlet代码的?
- Leetcode 282. Expression Add Operators
- 数据结构与算法学习(五)HashMap
- C++将10进制字符串转为16进制字符串