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(≤). 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
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ;
int tempOri[maxn],origin[maxn],change[maxn];
int n; bool same(int A[],int B[]){
for(int i = ; i < n; i++){
if(A[i] != B[i]){
return false;
}
}
return true;
} void showArray(int A[]){
for(int i = ; i < n ; i++){
printf("%d",A[i]);
if(i < n - ) printf(" ");
}
printf("\n");
} bool insert(){
bool flag = false;
for(int i = ; i < n; i++){
if(i != && same(tempOri,change)){
flag = true;
}
int temp = tempOri[i], j = i;
while(j > && tempOri[j - ] > temp){
tempOri[j] = tempOri[j-];
j--;
}
tempOri[j] = temp;
if(flag == true){
return true;
}
}
return false;
} void Merge(){
bool flag = false;
for(int step = ; step/ <= n; step *= ){
if(step != && same(tempOri,change)){
flag = true;
}
for(int i = ; i < n; i += step){
sort(tempOri+i,tempOri+min(i+step,n));
}
if(flag == true){
showArray(tempOri);
return;
}
}
} int main(){
scanf("%d",&n);
for(int i = ; i < n; i++){
scanf("%d",&origin[i]);
tempOri[i] = origin[i];
}
for(int i = ; i < n ; i++){
scanf("%d",&change[i]);
}
if(insert()){
printf("Insertion Sort\n");
showArray(tempOri);
}else{
printf("Merge Sort\n");
for(int i = ; i < n; i++){
tempOri[i] = origin[i];
}
Merge();
}
return ;
}
1089 Insert or Merge(25 分)的更多相关文章
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PTA 09-排序2 Insert or Merge (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge (25分) According to ...
- PAT 1089. Insert or Merge (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089. Insert or Merge (25)
题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...
- 1089. Insert or Merge (25)-判断插入排序还是归并排序
判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...
- PAT (Advanced Level) 1089. Insert or Merge (25)
简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- A1089 Insert or Merge (25 分)
一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
随机推荐
- [CJOJ2425][SYZOI Round1]滑稽的树
cjoj sol 子树转化成dfs序上的区间. 所以就变成了:区间Kth,区间内[a,b]范围内的数有多少个,单点修改 裸的树套树啊. code #include<cstdio> #inc ...
- openfire存储中文字符乱码解决办法
转载于: Xmpp问题总结:处理Openfire 中文乱码问题(2) openfire是一个非常不错的IM服务器,而且是纯Java实现,具有多个平台的版本,他的数据存储可以采用多种数据库,如MySQL ...
- java web项目创建
https://www.cnblogs.com/kangjianwei101/p/5621738.html
- ios判断是否为iphone6或iphone6plus代码
转自:http://blog.csdn.net/lvxiangan/article/details/45288505 #define IS_IPAD (UI_USER_INTERFACE_IDIOM( ...
- char与wchar_t数据类型
转自:http://blog.itpub.net/27634692/viewspace-752200/ 有的人爱用strcpy等标准ANSI函数,有的人爱用_tXXXX函数,有必要把来龙去脉搞清楚. ...
- java继承示例
package day07; class Fu { int num = 5; } class Zi extends Fu { int num =7; void show() { int num =9; ...
- C#中打开文件、目录、保存窗口
打开文件代码: try { OpenFileDialog of = new OpenFileDialog(); of.ShowDialog(); txt_destFilePath.Text = of. ...
- [FATAL_ERROR] Uncaught PDOException: There is already an active transaction
[FATAL_ERROR] Uncaught PDOException: There is already an active transaction ... $mysql->beginTran ...
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- [hdu4662]MU Puzzle(找规律)
题意:给你一个串MI,按照三种规则1:M后面的部分加倍 2:III->U 3:删去连续的两个UU.看看能否变为给定的串 解题关键:将所有的U转化为I,发现 t+k*6=2^i -> =2^ ...