PAT - B 1035 Insertion and Merge (Sort)

1035. Insertion and Merge(25)

time limit
200 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue

According to Wikipedia's definition:

Insertion sort is an iterative algorithm that obtains input data one by one and gradually produces an ordered output sequence. In each iteration, the algorithm takes an element from the input sequence and inserts it into the correct position in the ordered sequence. Iterate in this way until all elements are in order.

Merge sort performs the following iterative operations: first, the original sequence is regarded as N ordered subsequences containing only one element, and then two adjacent ordered subsequences are merged in each iteration until only one ordered subsequence is left at the end the sequence of.

Now given the original sequence and the intermediate sequence generated by a sorting algorithm, please judge which sorting algorithm is the algorithm?

Input format:

The input gives the positive integer N (<=100) on the first line; the N integers of the original sequence are given on the next line; the intermediate sequence produced by some sorting algorithm is given on the last line. It is assumed here that the target sequence of sorting is ascending order. The numbers are separated by spaces.

Output format:

First, output "Insertion Sort" in line 1 for insertion sort, or "Merge Sort" for merge sort; then in line 2, output the result sequence of one more iteration of the sorting algorithm. Questions ensure that the results of each set of tests are unique. Numbers are separated by spaces, and there must be no extra spaces at the end of the line.

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

Idea: First find out the difference between merge sort and insertion sort 

          题目中说:答案唯一,所以不会有既是归并又是插入 这种有二义性的输入。

          所以 归并与插入的不同在于 插入前面是有序但后面则跟原始序列相同,而归并则不是。

#include<cstdio>
#include<vector>
#include<algorithm> 
using namespace std;

int main(){
	//freopen("input.txt", "r", stdin);
	vector<int> v1,v2;
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		v1.push_back(a);
	}
	for(int i = 0; i < n; i++){
		int a;
		scanf("%d", &a);
		v2.push_back(a);
	}
	int flag = 0;
	int k;
	for(int i = 0; i < n-1; i++){
		if(v2[i] <= v2[i+1]) continue;
		else{
			k = i + 1;
			for(int j = i+1; j < n; j++){
				if(v2[j] != v1[j]){ //这里只要有与原始序列不同的 就跳出 说明是归并
					flag = 1;
					break;
				}
			}
			if(!flag) break;
		}
	}
	if(flag){
		printf("Merge Sort\n");
		int cnt = 1, min;
		for(int i = 2; i < n; i*=2){   //从长度=2开始排序
			int j;
			for(j = 0; j < n; j+=i)
			    if(i + j <= n)
				    sort(v1.begin() + j, v1.begin() + i + j);
			    else sort(v1.begin() + j, v1.end()); // remaining sort
			if(v2 == v1){ //If the same, jump out
				cnt = i * 2; //length of the next sort
				break;
			}
		}
		if(cnt  >= n){
		    sort(v2.begin(), v2.end()); // all direct sorting
		}
		else{
			for(int i = 0; i < n; i += cnt){
				if(i + cnt <= n)
				    sort(v2.begin() + i, v2.begin() + i + cnt);
				else sort(v2.begin() + i, v2.end()); // remaining sort
			}
		}
	}
	else{
		printf("Insertion Sort\n");
		sort(v2.begin(), v2.begin() + k + 1);
	}
	printf("%d", v2[0]);
	for(int i = 1; i < n; i++){
		printf(" %d", v2[i]);
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763592&siteId=291194637