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:10 3 1 2 8 7 5 9 4 6 0 1 2 3 7 8 5 9 4 6 0Sample Output 1:
Insertion Sort 1 2 3 5 7 8 9 4 6 0Sample Input 2:
10 3 1 2 8 7 5 9 4 0 6 1 3 2 8 5 7 4 9 0 6Sample Output 2:
Merge Sort 1 2 3 8 4 5 7 9 0 6
输入:
N(个数总和)
原始数据队
排序到一半的队
要求回答是插入排序还是归并排序,并再给出对应操作的一步。
【直接插入Insert sort 】,当N>1时;进行N-1循环,每次的主角为2~N;每次把主角和与他前一个位置的配角比较,如果主角较小,把主角当前位置给与他比较的配角,并占据配角的位置,继续直到前面没有配角;否则这一次结束
显然前面排好序的一定从小到达,后面还没有排好序的一定在原来的位置;
【归并排序】: 先两个分成一组排序;接着4个分成一组排序,再接着八个分成一组排序;……最后的N个排序;
显然每组的个数要么2的幂要么为N;
我通过一次循环算每段从小到大排好的元素个数最少的【但不包括最后的那段】当作是大于或等于已经进行的markindex个分成一组;显然这个是不精确的,但是可以根据这个不精确的数获得下一步需要分成index一组排序(如果index>N,那么一次merge会自动调整为N);
不包括最后一段是 比如 1 2 5 6 3 4,显然最后一步可以直接就N了;
1 2 3 7 8 9 5 6 4 一开是的markindex是6,但是需要调整为2;那么会获得接着的index是4
联动 Insertion or Heap Sort: http://xujiayu317.blog.163.com/blog/static/25475209201573115911922/
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月18日 20:06 | 答案正确 | 25 | 1089 | C++ (g++ 4.7.2) | 1 | 308 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 252 | 7/7 |
1 | 答案正确 | 1 | 180 | 6/6 |
2 | 答案正确 | 1 | 304 | 1/1 |
3 | 答案正确 | 1 | 308 | 1/1 |
4 | 答案正确 | 1 | 180 | 3/3 |
5 | 答案正确 | 1 | 308 | 4/4 |
6 | 答案正确 | 1 | 308 | 3/3 |
#include<iostream> #include<vector> #include<algorithm> using namespace std; bool readln(vector<int>*now, int N,int *markindex) { int index; bool nodecrease= true; bool IsInsert = true; vector<int>sequence(N); for (index = 0; index < N; index++) cin >> sequence[index]; for (index = 0; index < N; index++) { cin >> (*now)[index]; if (index>0) if (nodecrease&& (*now)[index] >=(*now)[index - 1]) (*markindex) = index; else { nodecrease = false; if (IsInsert&&(*now)[index] != sequence[index]) IsInsert = false; } } return IsInsert; } void Insertone(vector<int>*now, int markindex) { int temp =(*now) [markindex+1]; while (markindex >= 0 && temp < (*now)[markindex]) { (*now)[markindex+1] = (*now)[markindex]; markindex--; } (*now)[markindex+1] = temp; } bool mergecmp(const int&A, const int &B){ return A < B; } void mergesort(vector<int>*now, int N,int markindex) { int index,temp; markindex++; for (temp=index = markindex; index < N; index++) { if (temp<index&&(*now)[index] < (*now)[index-1]) { if (index - temp < markindex&&index!=N-1)markindex = index - temp; temp = index; } } index = 1; while(index<=markindex)index *=2; for (temp=0;temp<N;temp = markindex) { markindex = temp + index < N ? temp + index : N; sort((*now).begin()+temp, (*now).begin()+markindex,mergecmp); } } void display(vector<int>*now, int N) { cout << (*now)[0]; for (int index = 1; index < N; index++) cout << " " << (*now)[index]; cout << endl; } int main() { int N,markindex=0; cin >> N; vector<int>now(N); if (readln( &now, N,&markindex)) { if (markindex+1<N) Insertone(&now,markindex); cout << "Insertion Sort" << endl; } else { mergesort(&now, N, markindex); cout << "Merge Sort" << endl; } display(&now, N); system("pause"); return 0; }