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 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 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


提交代码

这里的插入排序和非递归的归并,要注意。

  1 #include<cstdio>
  2 #include<cmath>
  3 #include<cstring>
  4 #include<stack>
  5 #include<algorithm>
  6 #include<iostream>
  7 #include<stack>
  8 #include<set>
  9 #include<map>
 10 #include<vector>
 11 #include<queue>
 12 using namespace std;
 13 int line[105],order[105],line1[105];
 14 /*void HeapSort(int *order,int n){
 15     int i=n-1;
 16     while(order[i]>order[(i-1)/2]){
 17         i--;
 18     }
 19     int temp=order[i];
 20     order[i]=order[0];
 21     int j=1;//最小左节点
 22     for(;j<i;j=2*j+1){
 23         if(j+1<i&&order[j+1]>order[j]){
 24             j++;
 25         }
 26         if(order[j]>temp){
 27             order[(j-1)/2]=order[j];
 28         }
 29     }
 30     order[(j-1)/2]=temp;
 31 }*/
 32 void Merge(int order[],int temp[],int l,int r,int e){
 33     int i=l,j=r,p=l;
 34     while(i<r&&j<=e){
 35         if(order[i]<order[j]){
 36             temp[p++]=order[i++];
 37         }
 38         else{
 39             temp[p++]=order[j++];
 40         }
 41     }
 42     while(i<r){
 43         temp[p++]=order[i++];
 44     }
 45     while(j<=e){
 46         temp[p++]=order[j++];
 47     }
 48 }
 49 void Merge_pass(int order[],int temp[],int n,int len){
 50     int i;
 51     for(i=0;i<=n-2*len;i+=2*len){
 52         Merge(order,temp,i,i+len,i+2*len-1);
 53     }
 54     if(i+len<n){
 55         Merge(order,temp,i,i+len,n-1);//Merge(i,i+len,n-1);
 56     }
 57     else{
 58         while(i<n){
 59             temp[i]=order[i];
 60             i++;
 61         }
 62     }
 63 }
 64 void MergeSort(int *line,int *order,int n){
 65     int *temp=new int[n+1];
 66     int len=1,i;
 67     while(len<n){
 68         Merge_pass(line,temp,n,len);
 69         for(i=0;i<n;i++){
 70             line[i]=temp[i];
 71             //cout<<i<<" "<<line[i]<<endl;
 72         }
 73         for(i=0;i<n;i++){
 74             if(line[i]!=order[i]){
 75                 break;
 76             }
 77         }
 78         if(i==n){
 79             break;
 80         }
 81         len*=2;
 82     }
 83     if(len<n){
 84         Merge_pass(temp,order,n,len*2);
 85         /*for(i=0;i<n;i++){
 86             cout<<i<<" "<<order[i]<<endl;
 87         }*/
 88     }
 89     delete []temp;
 90 }
 91 int main()
 92 {
 93     //freopen("D:\\INPUT.txt","r",stdin);
 94     int n;
 95     scanf("%d",&n);
 96     int i,j;
 97     for(i=0;i<n;i++){
 98         scanf("%d",&line[i]);
 99         line1[i]=line[i];
100     }
101     for(i=0;i<n;i++){
102         scanf("%d",&order[i]);
103     }
104     for(i=1;i<n;i++){
105         int temp=line[i];
106         for(j=i;j>=1&&line[j-1]>temp;j--){
107             line[j]=line[j-1];
108         }
109         line[j]=temp;
110         for(j=0;j<n;j++){
111             if(line[j]!=order[j]){
112                 break;
113             }
114         }
115         if(j==n){
116             break;
117         }
118     }
119     if(i==n){//merge sort
120         printf("Merge Sort\n");
121         MergeSort(line1,order,n);
122     }
123     else{//insertion sort
124         printf("Insertion Sort\n");
125         i++;
126         int temp=order[i];
127         for(j=i;j>=1&&order[j-1]>temp;j--){//i一定要指向最后
128             order[j]=order[j-1];
129         }
130         order[j]=temp;
131     }
132     printf("%d",order[0]);
133     for(i=1;i<n;i++){
134         printf(" %d",order[i]);
135     }
136     printf("\n");
137     return 0;
138 }
时间: 2024-10-27 06:38:12

pat1089. Insert or Merge (25)的相关文章

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

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

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

PAT1089. Insert or Merge

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

PAT (Advanced Level) 1089. Insert or Merge (25)

简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn=200; int a[maxn],b[maxn],

PAT 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 li

PAT 1088 1089. Insert or Merge (25)(排序啊)

题目链接:http://www.patest.cn/contests/pat-a-practise/1089 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 d