【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意:
输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列。输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操作所得到的序列。数据保证了不会有两种排序方式都可以得到的序列。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
int b[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i)
cin>>a[i];
for(int i=;i<=n;++i)
cin>>b[i];
int pos=;
for(int i=n;i;--i)
if(a[i]!=b[i]){
pos=i;
break;
}
int flag=;
int loc=;
for(int i=;i<n;++i)
if(b[i]>b[i+]){
loc=i;
break;
}
for(int i=loc+;i<=n;++i)
if(a[i]!=b[i]){
flag=;
}
if(flag){
cout<<"Insertion Sort\n";
sort(a+,a++loc+);
for(int i=;i<=n;++i){
cout<<a[i];
if(i<n)
cout<<" ";
}
}
else{
cout<<"Merge Sort\n";
int t=;
while(){
int flag=;
for(int i=;i<=n;++i)
if(a[i]!=b[i]){
flag=;
break;
}
t*=;
for(int i=;i<=n/t;++i)
sort(a+(i-)*t+,a+i*t+);
sort(a+n/t*t+,a+n+);
if(!flag)
break;
}
for(int i=;i<=n;++i){
cout<<a[i];
if(i<n)
cout<<" ";
}
}
return ;
}
【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)的更多相关文章
- 1089. Insert or Merge (25)-判断插入排序还是归并排序
判断插入排序很好判断,不是的话那就是归并排序了. 由于归并排序区间是2.4.8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举. 然后再对每个子区间进行一下sort即可. #inc ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- PAT Advanced 1089 Insert or Merge (25) [two pointers]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 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, 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 甲级 1083 List Grades (25 分)
1083 List Grades (25 分) Given a list of N student records with name, ID and grade. You are supposed ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
随机推荐
- MyEclipse设置不编译js部分
https://jingyan.baidu.com/album/ca41422fe094251eae99ede7.html?picindex=1 步骤: 1)选中当前工程,右键单击properties ...
- bitlocker对磁盘进行加密解密
1,bitlocker是什么? BitLocker即Windows BitLocker驱动器加密.是微软在Windows Vista中新增的一种数据保护功能.使用BitLocker可以加密磁盘.主要用 ...
- JavaScript 开胃菜
注释 单行注释:// 快捷键: CTRL + / 多行注释: /* 内容 */ 快捷键: ctrl + shift + / 变量 申明变量 var name; 赋值 name = 'peach'; 初 ...
- Vue中美元$符号的意思
vue的实例属性和方法 除了数据属性,Vue 实例还暴露了一些有用的实例属性与方法.它们都有前缀 $,以便与用户定义的属性区分开来.例如: var data = { a: 1 } var vm = n ...
- node安装升级过程中遇到的问题汇总
一.Node already installed, it's just not linked 第一步:sudo chown -R $(whoami) $(brew --prefix)/* 第二步:br ...
- python之文件复制
python文件复制操作. # -*- coding: utf-8 -*- import shutil import os # file_path = 'C:\\Users\\WT\\Desktop\ ...
- 题解【CJOJ2608】[JZOJ 100043]第k小数
P2608 - [JZOJ 100043]第k小数 Description 有两个非负整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到N*M个数,询问这N*M个数中第K ...
- 题解【洛谷P1645/CJOJ1244】序列
P1645 序列 Description 有一个整数序列,它的每个数各不相同,我们不知道它的长度(即整数个数),但我们知道在某些区间中至少有多少个整数,用区间(Li,Ri,Ci)来描述,表示这个整数序 ...
- mvn + testng + allure 生成自动化测试报告
最近学了个新东西,使用java的testng测试框架做自动化测试.并且声称自动化报告. (1)创建maven工程 File-New-Other (2)创建testng类 当前import org.te ...
- Zigbee 与 WiFi 的区别
zigbee 在蓝牙技术的使用过程中,人们发现蓝牙技术尽管有许多优点,但仍存在许多缺陷.对工业,家庭自动化控制和遥测遥控领域而言,蓝牙技术显得太复杂,功耗大,距离近,组网规模太小等,……而工业自动化对 ...