Pat(Advanced Level)Practice--1089(Insert or Merge )

Pat1089代码

题目描述:

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

AC代码:

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define MAXN 105

using namespace std;

int original[MAXN],temp[MAXN],ans[MAXN];
int n;

bool isEqual(int a[],int b[]){
	for(int i=0;i<n;i++){
		if(a[i]!=b[i])
			return false;
	}
	return true;
}

void output(int a[]){
	printf("%d",a[0]);
	for(int i=1;i<n;i++)
		printf(" %d",a[i]);
	printf("\n");
}

bool isInsert(){
	int flag=0;
	for(int i=2;i<=n;i++){
		if(flag&&!isEqual(ans,temp)){
			printf("Insertion Sort\n");
			output(ans);
			return true;
		}
		sort(ans,ans+i);
		if(isEqual(ans,temp))
			flag=1;
	}
	return false;
}

bool isMerge(){
	int flag=0;
	int i=0,j=0;
	for(i=2;i<=n;i=i*2){
		for(j=0;j+i<=n;j=j+i){
			if(isEqual(original,temp))
				flag=1;
			sort(original+j,original+j+i);
		}
		sort(original+j,original+n);//remaining sublist which has less than i elements
		if(isEqual(original,temp))
			flag=1;
		if(flag&&!isEqual(original,temp)){
			printf("Merge Sort\n");
			output(original);
			return true;
		}
	}
	return false;
}

int main(int argc,char *argv[]){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&original[i]);
		ans[i]=original[i];
	}
	for(int i=0;i<n;i++)
		scanf("%d",&temp[i]);
	if(!isInsert())
		isMerge();
	return 0;
}

另一种方法:一种直观的解法,可以分别写出插入排序和归并排序,然后比较每一次迭代后的顺序;

时间: 2024-10-04 15:38:05

Pat(Advanced Level)Practice--1089(Insert or Merge )的相关文章

1002 A+B for Polynomials (PAT (Advanced Level) Practice)

This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N?1?? a?N?1???? N?2?? a?N?2?

PAT (Advanced Level) Practice 1011 World Cup Betting (20 分)

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their

PAT (Advanced Level) Practice 1068 Find More Coins

题解 01背包板子 + 记录路径.这次的记录路径比较特殊,要从多组解中找到一组由尽量小价值的硬币组成的解.所以不能利用一维数组记录路径,path[目前重量] = 物品序号,因为这样最后只能记录一个可能符合或不符合要求解.所以应该利用二维数组记录路径,path[ 物品序号 ][ 目前重量 ] = 1,这样可以记录多组解.因为要求为找到最小的一组解,所以先将拥有的硬币从大到小排序,以便于进行01背包时,可以从大到小更新解. 代码 #include<bits/stdc++.h> using name

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

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi