浙大pat | 浙大pat 牛客网甲级1061 Insert or Merge (25)判断是插入排序还是归并排序

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, andgrowing a sorted output list. Each iteration,

insertion sort removes one element from the input data, finds the location itbelongs 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 containing1 element (a list of 1 element is

considered sorted). Then repeatedly merge two adjacent sublists to produce newsorted sublists until there is only 1

sublist remaining.



Now given the initial sequence of integers, together with a sequence which is aresult of several iterations of some

sorting method, can you tell which sorting method we are using?



输入描述:

Each input file contains one test case. For each case, the firstline gives a positive integer N (<=100). Then in the next line, N integersare

given as the initial sequence. The last line contains the partially sortedsequence 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.




输出描述:

For each test case, print in the first line either"Insertion Sort" or "Merge Sort" to indicate the methodused to obtain the partial result.

Then run this method for one more iteration and output in the second line theresulting sequence. It is guaranteed that the answer is

unique for each test case. All the numbers in a line must be separated by aspace, and there must be no extra space at the end of the

line.



输入例子:

10

3 1 2 8 7 5 9 4 6 0

1 2 3 7 8 5 9 4 6 0



输出例子:

Insertion Sort

1 2 3 5 7 8 9 4 6 0

这题真的是,不看答案估计没什么人做的出来

首先题目中有一个条件没有给,就是归并排序的一次迭代到底定义为什么,一次迭代既可以定义为将两个相邻的子序列合并,也可以定义为将当前步长所有子序列合并,这一题将归并排序的一次迭代定义为后者,但是我觉得应该是前者

题目中还有一个隐藏条件就是他说不会有一组数据即可能是归并排序有可能是插入排序

我们考虑从左到右找到第一个无序的数字,假如从这个无序的数字开始,一直到数组结束,都和原数组相同,那么这个序列一定可以由插入排序得到,题目中说了,数据不可能既是插入排序又是归并排序,因此这个数据不可能是归并排序,题目会抹除一切可能

从左到右找到第一个无序的数字,假如从这个无序的数字开始,一直到数组结束,和原数组相同,那么这个序列一定不能有插入排序得到,那么这个序列就一定是归并排序,题目数据会保证这一点   

以后在做这种题的时候用一定有可能是A那么就一定是A,一定不可能是A那么就一定是B来做会简单一些

#include <iostream>
#include <string>
#include<algorithm>
using namespace std;

int theOther[103];
int num[103];
int main()
{
	int N;
	cin >> N;
	string whatSide;
	int tmp;
	for (int i = 0; i < N; i++)
	{
		cin >> theOther[i];
	}
	for (int i = 0; i < N; i++)
	{
		cin >> num[i];
	}

	for (int i = 1; i < N; i++)
	{
		if (num[i] < num[i - 1])
		{
			int j = i;
			tmp = i;
			for (; j < N; j++)
				if (theOther[j] == num[j]) continue;
				else break;

			if (j == N) whatSide = "Insertion Sort";
			else whatSide = "Merge Sort";
			break;
		}
	}
	if (whatSide == "Insertion Sort")
	{
		for (int i = tmp; i >= 0; i--)
		{
			if (num[i] < num[i - 1])
			{
				swap(num[i], num[i - 1]);
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		int length = 2;
		int start = 0;
		int index = start+1;
		for (; length < N * 2; length *= 2)
		{
			start = 0;
			index = start + 1;
			bool theFind = false;
			while (1)
			{
				while (index < start + length&&index < N)
				{
					if (num[index] >= num[index - 1]) { index++; continue; }
					else
					{
						theFind = true;
						break;
					}
				}
				if (theFind)
				{
					for(int i=start;i<N;i+=length)
					sort(num + i, num +min(i+length,N));
					goto allEnd;
				}
				if (index >= N) break;
				start += length; index = start + 1;
			}
		}
	allEnd:;
	}
	cout << whatSide << endl;
	for (int i = 0; i < N; i++)
	{
		cout << num[i];
		if (i != N - 1) cout << " ";
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值