1089 Insert or Merge (25分)

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 (≤). 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 resuling 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

上一次碰排序怕不是一年半之前了,已经忘记的一塌糊涂了。。。
这道题一上来首先想出了如何区别两个排序,然后这里居然还写的有一个样例过不了...真是菜
归并排序那里,由于已经很久没写了,甚至还返回去看一下之前写的博客,总想着要怎么实现,
看了一下发现是递归实现的,就感觉不止25分,然后去搜了一下网上的答案,sort。卒。
想到了开头insertion的sort,可是到了自己虚弱的merge就害怕了,老想着怎么实现,然后半路
截断输出,真是越怕什么就越容易输给什么。。。
到时候复习数据结构的时候,接着,"今天排序,明天搜索,后天dp/xyx"
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int maxn = 100 + 5;
 6 int val[maxn];
 7 int sorted[maxn];
 8 int n;
 9 
10 void insert(int k) {
11     sort(sorted, sorted + k + 2);
12     for(int i = 0; i < n; i ++) {
13         val[i] = sorted[i];
14     }
15 }
16 
17 void merge() {
18     int k = 1;
19     bool flag = true;
20     while(flag) {
21         flag = false;
22         for(int i = 0; i < n; i ++) {
23             if(val[i] != sorted[i]) {
24                 flag = true;;
25             }
26         }
27         k = k * 2;
28         for(int i = 0; i < n / k; i ++) {
29             sort(val + i * k, val + (i + 1) * k);
30         }
31         sort(val + n / k * k, val + n);
32     }
33 }
34 
35 int main() {
36     scanf("%d", &n);
37     for(int i = 0; i < n; i ++) {
38         scanf("%d", &val[i]);
39     }
40     for(int i = 0; i < n; i ++) {
41         scanf("%d", &sorted[i]);
42     }
43     int k, j;
44     for(k = 0; k < n - 1 && sorted[k] <= sorted[k + 1]; k ++);
45     for(j = k + 1; j < n && val[j] == sorted[j]; j ++);
46     if(j == n) {
47         printf("Insertion Sort
");
48         insert(k);
49     } else {
50         printf("Merge Sort
");
51         merge();
52    }
53    for(int i = 0; i < n; i ++) {
54         if(i) printf(" ");
55         printf("%d", val[i]);
56     }
57     printf("
");
58     return 0;
59 }
 
原文地址:https://www.cnblogs.com/bianjunting/p/13173632.html