【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意:
输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组。判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一)。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
int c[][];
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=;i<n;++i)
if(b[i]>b[i+]){
pos=i;
break;
}
int pos2=;
for(int i=pos+;i<=n;++i)
if(a[i]!=b[i]){
pos2=i;
break;
}
if(!pos2){
cout<<"Insertion Sort\n";
int flag=,mn=b[pos+];
int ppos=;
for(int i=;i<=n;++i)
if(b[i]>mn){
ppos=i;
break;
}
for(int i=pos+;i>ppos;--i)
b[i]=b[i-];
b[ppos]=mn;
for(int i=;i<=n;++i){
cout<<b[i];
if(i<n)
cout<<" ";
}
}
else{
cout<<"Heap Sort\n";
int ppos=;
sort(a+,a++n);
for(int i=n;i;--i)
if(b[i]!=a[i]){
ppos=i;
break;
}
int mx=,flag=;
for(int i=;i<=ppos;++i)
if(b[i]>mx){
mx=b[i];
flag=i;
}
swap(b[flag],b[ppos]);
int s=;
while(s<ppos){
if(b[s*]>b[s]&&b[s*+]>b[s]&&ppos>s*+){
if(b[s*]>b[s*+]){
swap(b[s],b[s*]);
s=s*;
}
else{
swap(b[s],b[s*+]);
s=s*+;
}
}
else if(b[s*+]>b[s]&&ppos>s*+){
swap(b[s],b[s*+]);
s=s*+;
}
else if(b[s*]>b[s]&&ppos>s*){
swap(b[s],b[s*]);
s=s*;
}
if(s*>=ppos)
break;
}
for(int i=;i<=n;++i){
cout<<b[i];
if(i<n)
cout<<" ";
}
}
return ;
}
【PAT甲级】1098 Insertion or Heap Sort (25 分)的更多相关文章
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT甲级1098. Insertion or Heap Sort
PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...
- PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...
- PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- 1098 Insertion or Heap Sort (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PTA 09-排序3 Insertion or Heap Sort (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort (25分) Accor ...
- PAT (Advanced Level) Practise - 1098. Insertion or Heap Sort (25)
http://www.patest.cn/contests/pat-a-practise/1098 According to Wikipedia: Insertion sort iterates, c ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
随机推荐
- 完整安装IIS服务
此文主要是针对前面提到的 IIS支持json.geojson文件 添加脚本映射时,提示找不到asp.dll时的解决方法. 主要参考了此文:http://www.kodyaz.com/articles/ ...
- oracle 处理锁表,创建新的数据库实例
select saddr,sid,serial#,paddr,username,status from v$session where username is not null and usernam ...
- codeforces 1284D. New Year and Conference(线段树)
链接:https://codeforces.com/problemset/problem/1284/D 题意:有n场讲座,有两个场地a和b,如果在a场地开讲座则需要占用[sai,eai],在b场地开讲 ...
- docker命令总结(一)
个人简单总结: 参数 用途 语法 示例 search 在docker hub中搜索镜像 docker search 镜像名称 docker search nginx pull 在docker hub中 ...
- Vue路由跳转时显示空白页面,iview的使用
最近在用iview做项目,需要实现登录,注册,忘记密码等功能.iview-admin本来就有登录功能,于是想照葫芦画瓢,实现登录界面的注册,忘记密码页面路由跳转. router.js里路由配置,刚开始 ...
- Docker常用命令及脚本
1.常用命令 docker search centos #搜索镜像 pull centos ...
- 517,sytlus/sass/less的区别
均具有“变量”,“混合”,“嵌套”,“继承”,“颜色混合”五大基本特性 scss和less语法较为严禁,less要求一定要使用大括号“{}”,scss和stylus可以通过缩进表示层次与嵌套关系 sc ...
- 【C语言】一堆数组中存放了10个小于100的整数,请编程对所有数据按照从小到大的顺序进行排序,若个位数相等,则按照十位从小到大的顺序排序,输出排序后的结果
分析:取余,判断个位是否相等,利用冒泡法排序 #include <stdio.h> int main() { ] = { ,,,,,,,,, };/*数组*/ int i, j, k; ; ...
- Integer.parseInt(s)、Integer.valueOf(s)与new Integer()的异同
我们在开发过程中,很多时候需要将String类型数据转换成Integer,而比较常用的方式就是--nteger.parseInt(s).Integer.valueOf(s)与new Integer() ...
- 三分钟快速上手TensorFlow 2.0 (上)——前置基础、模型建立与可视化
本文学习笔记参照来源:https://tf.wiki/zh/basic/basic.html 学习笔记类似提纲,具体细节参照上文链接 一些前置的基础 随机数 tf.random uniform(sha ...