PAT_A1089#Insert or Merge

Source:

PAT A1089 Insert or Merge (25 分)

Description:

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

Keys:

  • 插入排序和归并排序

Attention:

  • 见注释

Code:

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 const int M=1e3;
 5
 6 int main()
 7 {
 8 #ifdef ONLINE_JUDGE
 9 #else
10     freopen("Test.txt", "r", stdin);
11 #endif // ONLINE_JUDGE
12
13     int n,origin[M],sorted[M];
14     scanf("%d", &n);
15     for(int i=0; i<n; i++)
16         scanf("%d", &origin[i]);
17     for(int i=0; i<n; i++)
18         scanf("%d", &sorted[i]);
19     int pos=1;
20     while(pos<n && sorted[pos-1]<=sorted[pos])  //可以取等
21         pos++;
22     int pt=pos;
23     while(pos<n && sorted[pos]==origin[pos])
24         pos++;
25     if(pos == n)
26     {
27         printf("Insertion Sort\n");
28         while(pt>0 && sorted[pt-1]>sorted[pt])
29         {
30             swap(sorted[pt-1],sorted[pt]);
31             pt--;
32         }
33     }
34     else
35     {
36         printf("Merge Sort\n");
37         pos=1;pt=1;
38         while(pt)
39         {
40             pos *= 2;
41             for(int i=0; i<n; i+=pos)
42             {
43                 for(int j=i+1; j<min(n,i+pos); j++)
44                     if(sorted[j-1]>sorted[j])
45                         pt=0;
46             }
47         }
48         for(int i=0; i<n; i+=pos)
49             sort(sorted+i,sorted+min(i+pos,n));
50     }
51     for(int i=0; i<n; i++)
52         printf("%d%c", sorted[i], i==n-1?‘\n‘:‘ ‘);
53
54     return 0;
55 }

原文地址:https://www.cnblogs.com/blue-lin/p/11408491.html

时间: 2024-10-13 16:03:19

PAT_A1089#Insert or Merge的相关文章

pat1089. Insert or Merge (25)

1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion s

60. Insert Interval &amp;&amp; Merge Intervals

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i

Insert or Merge

h2.western { font-family: "Liberation Sans", sans-serif; font-size: 16pt } h2.cjk { font-size: 16pt } h2.ctl { font-size: 16pt } h1 { margin-bottom: 0.21cm } h1.western { font-family: "Liberation Sans", sans-serif; font-size: 18pt } h1

PTA 09-排序2 Insert or Merge (25分)

题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 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 rem

1089. Insert or Merge (25)【排序】——PAT (Advanced Level) Practise

题目信息 1089. Insert or Merge (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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, find

1089. Insert or Merge (25)

1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion s

07-2. Insert or Merge (25)

07-2. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion so

A1089. 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 li

Insert Interval &amp; Merge Intervals

Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary). Example Insert [2, 5] into [[1,2], [5,9]],