1098 Insertion or Heap Sort (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.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

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 Nnumbers. 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 "Heap 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.

* 注意标记行

 #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ; int n;
int a[maxn];
int b[maxn]; void InsertSort(int v){
int tmp=b[v], i;
for(i=v-;i>=;i--){
if(b[i]>tmp){
b[i+]=b[i];
}else{
break;
}
}
b[i+] = tmp; for(int i=;i<n;i++){
if(i!=) printf(" ");
printf("%d",b[i]);
}
} void HeapSort(int v){ // MAX HEAP
int tmp=b[];
b[]=b[v];
b[v]=tmp;
tmp = b[];
int i=, child;
for(;(i+)*-<=v-; i=child){
child = (i+)*-;
if(b[child+]>b[child] && child+<=v-){
child++;
}
if(tmp<b[child]){
b[i]=b[child];
}else{
break;
}
}
b[i] = tmp;
for(int i=;i<n;i++){
if(i!=) printf(" ");
printf("%d",b[i]);
}
} void Judge(){
int i,ptr;;
for(i=;i<n&&b[i-]<=b[i];i++); // !!!注意这里
ptr=i;
for(; i<n&&a[i]==b[i];i++);
if(i==n){
printf("Insertion Sort\n");
InsertSort(ptr);
}else{
sort(a, a+n);
for(i=n-;i>=&&a[i]==b[i];i--);
printf("Heap Sort\n");
HeapSort(i);
}
} int main(){
cin>>n;
for(int i=; i<n; i++){
scanf("%d",&a[i]);
}
for(int i=; i<n; i++){
scanf("%d",&b[i]);
}
Judge();
}

1098 Insertion or Heap Sort的更多相关文章

  1. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

  2. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  3. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. 1098 Insertion or Heap Sort——PAT甲级真题

    1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  5. 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 ...

  6. 1098. Insertion or Heap Sort (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. PAT (Advanced Level) 1098. Insertion or Heap Sort (25)

    简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...

  8. PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)

    题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...

  9. 1098 Insertion or Heap Sort (25 分)(堆)

    这里的第二序列相当于是排序还没拍好的序列 对于第二个样例的第二个序列其实已经是大顶堆了 然后才进行的堆排序 知道这个就好做了 #include<bits/stdc++.h> using n ...

随机推荐

  1. js DomContentLoaded 和 load 的区别

    如题:DOMContentLoaded和load都是页面加载的时候触发的事件.区别在于触发的时机不一样. 浏览器渲染页面DOM文档加载的步骤: 1.解析HTML结构. 2.加载外部脚本和css文件. ...

  2. c#devexpress GridContorl添加进度条

    demo 的实现图 下边是步骤和代码 1定义 时钟事件,定时的增加进度条的增量. 2:  添加进度条 3;定义字段属性 using System; using System.Collections.G ...

  3. Oracle_SQL(5) 连接和子查询

    一.连接join一般分类: inner join: 内连接,又叫等值连接,只返回两个表中连接字段相等的行. left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right ...

  4. 去掉easyui datagrid内部虚线的方式。

    去掉easyui        datagrid内部虚线的方式.easyui datagrid的样式是统一写在样式文件中的,如果想要统一替换可以找对应的datagird样式文件中的以下部分.如果想要改 ...

  5. hdu 1558 (线段相交+并查集) Segment set

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...

  6. javascript 高级程序设计 十

    理解JS对象(2)创建对象   JS中创建对象的方式有很多,我们把他们统称为模式. 工厂模式: 优点:解决了创建多个相似对象的问题. 缺点:没有解决对象识别问题.(不知道一个实例对象的类型) func ...

  7. spring自动注解Autowired配置

    1.spring注解:http://blog.csdn.net/xyh820/article/details/7303330/ 2.最简ssm配置:http://blog.csdn.net/qq_18 ...

  8. Struts问题

    1.struts框架的5大组件:mvc,标签库,校验框架,国际化,tiles; 2.struts的9大核心类以及与mvc对应的关系: C ActionServlet RequestProcessor ...

  9. 社交类APP原型模板分享——QQ

    QQ是一款社交类的APP应用——聊天软件,支持多人群聊以及丰富有趣的娱乐功能. 此模板交互效果很丰富,主要有抽屉侧拉效果,滚动内容界面.标签组切换.选择组件触发按钮状态变化.点击下拉展开列表.点击弹出 ...

  10. tolua杂记

    1 字符串调用luaFunc  :DoString public class CallLuaFunction : MonoBehaviour { private string script = @&q ...