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

跟1098题十分相似。
但是和归并排序又不一样。归并算法直接递归,并且是对半分,很难捕捉到完整的序列进行判断。
所以用i = 2作为初始值,进行归并。上限是step值(也就是交界处)。每趟2倍递增。
然后每趟结尾与给出的结果序列比较。如果全部匹配,记下 i 值。结束循环。
最后以 i * 2 的值进行一趟的归并(借助sort),结果输出。

#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;
		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;
}

前辈传送门…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值