insertion or merge
来源:互联网 发布:网络流行歌曲2016 编辑:程序博客网 时间:2024/05/21 01:47
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
这里有个1分的坑,insertion和merge的顺序必须是insertion在前,merge在后,当上下两个序列相同的时候输出insertion?
#include<iostream> #include<vector>#include<algorithm>using namespace std;bool cmpvector(vector<int> a,vector<int> b){for(int i=0;i<a.size();i++){if(a[i]!=b[i]){return false;}}return true;}int main(){int N;cin>>N;vector<int> v1,v2,v3;for(int i=0;i<N;i++){int temp=0;cin>>temp;v1.push_back(temp);}for(int i=0;i<N;i++){int temp=0;cin>>temp;v2.push_back(temp);}v3=v1;int flag=0;for(int step=1;step<N;step++){if(cmpvector(v3,v2)&&step!=1){cout<<"Insertion Sort"<<endl;flag=1;}sort(v3.begin(),v3.begin()+(step)+1);if(flag){for(int i=0;i<v3.size()-1;i++){cout<<v3[i]<<' ';}cout<<v3[N-1]<<endl;return 0;}}int tag=0;for(int step=2;step/2<N;step*=2){if(cmpvector(v1,v2)){cout<<"Merge Sort"<<endl;tag=1;}for(int i=0;i<N;i+=step){sort(v1.begin()+i,v1.begin()+min(i+step,N));}if(tag){for(int i=0;i<v1.size()-1;i++){cout<<v1[i]<<' ';}cout<<v1[N-1]<<endl;return 0;}}return 0;}
- insertion or merge
- 1098. Insertion or Heap Sort (25) / 1089. Insert or Merge (25)
- 1098. Insertion or Heap Sort (25) 、 1089. Insert or Merge (25)
- 排序算法的判别: Insert or Merge,Insertion or Heap Sort
- Insertion Sort, Merge Sort
- Sort List (merge & Insertion)
- Insertion Sort and Merge Sort
- Insertion sort and Merge sort
- Insertion Sort and Merge Sort
- keras Merge or merge
- Detecting Media Insertion or Removal
- 1098.Insertion or Heap Sort
- 1098. Insertion or Heap Sort
- PAT1098 Insertion or Heap Sort
- 1098 Insertion or Heap Sort
- 1098. Insertion or Heap Sort
- pat1098. Insertion or Heap Sort
- Insertion or Heap Sort (25)
- mysql中geometry类型的简单使用
- 各种进制转换成十进制
- 电池曲线绘制算法
- 浅谈JSONP 的工作原理
- Python中的向量相加和numpy中的向量相加效率比较
- insertion or merge
- pwnable之brainfuck
- ajax请求后台返回数据data不能遍历
- linux 安装 MySQL ------------python 操作mysql
- 【bzoj4999】This Problem Is Too Simple!
- ajax异步表单提交,并实现文件上传
- 欢迎使用CSDN-markdown编辑器
- include指令与include动作之间的区别
- 高并发解决方案