PAT甲级1089
Posted wx630c98f24f6b8
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT甲级1089相关的知识,希望对你有一定的参考价值。
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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
int N; vector<int> v1,v2;
int n;
cin >> N;
for (int i = 0; i < N; i++)
cin >> n;
v1.push_back(n);
for (int i = 0; i < N; i++)
cin >> n;
v2.push_back(n);
bool flag = false;
int step = 0;
for (int i = 0; i < N-1; i++)
if (v2[i] <= v2[i + 1])
continue;
else
for (int j = i + 1; j < N; j++)
step = i+1;
if (v2[j] != v1[j])
flag = true;
break;//由于测试结果唯一,也就说不是插排就是归并,那么根据插排的规则,可知前几个必定有序
//后几个必定与未排序的原始序列后面部分相同
break;
if (flag)
bool flag1 = false;
cout << "Merge Sort" << endl;
int len = v2.size();
int i;//这个地方要注意,在step位置发现不保持有序了,并不代表step就是当前归并长度
//举个例子,1 2 3 4 7 8 5 6 0 9,这个序列是归并长度为2时的序列,但step会指到5
//因为8>5,不保持有序了,step = 6并不是2,那么对于归并的解决,我的做法是从归并长度
//为2开始往后对原始序列v1进行归并排序,每次归并后与当前的归并排序的序列v2的元素作比较
//若不等,归并长度翻倍继续进行归并,一直到两个序列完全相等,那么归并长度就能确定下来了。
for (i = 2; i <=step; i *= 2)
if(!flag1)
for (int j = 0; j < len; j += i)
sort(v1.begin() + j, v1.begin() + min( i + j, len));
int k;
for (k = 0; k < N; k++)
if (v1[k] == v2[k])
continue;
else
break;
if (k == N)
flag1 = true;
break;
if(flag1)
step = 2*i;
for (int i = 0; i < len; i+=step)
sort(v2.begin()+i, v2.begin() + min(step+i, len));
for (int i = 0; i < N; i++)
cout << v2[i];
if (i != N - 1)
cout << " ";
else
cout << endl;
else
cout << "Insertion Sort" << endl;
for (int i = step-1; i >= 0; i--)
if (v2[step] >= v2[i])
int temp = v2[step];
for (int j =step; j >i+1; j--)
v2[j] = v2[j-1];
v2[i+1] = temp;
break;
if (v2[step] < v2[0])//对于插排,注意当前元素小于之前所有元素的情况就行
int temp = v2[step];
for (int j = step; j > 0; j--)
v2[j] = v2[j - 1];
v2[0] = temp;
break;
for (int i = 0; i < N; i++)
cout << v2[i];
if (i != N - 1)
cout << " ";
else
cout << endl;
return 0;
以上是关于PAT甲级1089的主要内容,如果未能解决你的问题,请参考以下文章