题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列。

  插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同。

  堆排序的特点就是,后面是从小到大排列的最大的几个数p~n-1,前面第一位则是p-1。

  所以只要先按照插入排序的特点来判断是否为插入排序,接下来再进行操作就可以了,这里要手动写下最大堆的更新操作。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
/* 之前一直WA的原因是,用for循环写寻找idx一开始就写错了。。。
找了整个序列的<,应该是找反例>从而跳出for循环,或者直接加到条件里。
比如:
一开始这么写,
for(int i=0;i<n;i++){
if(num2[i]<=num2[i+1])
idx++;
}
正确应该是:
for(idx=0;idx<n-1 && num2[idx]<=num2[idx+1];idx++);
晕死。。。脑子糊涂了
*/
using namespace std;
const int maxn=;
int heap[maxn];
int heap_size=; void swaps(int &a,int &b){
int tmp;
tmp=a;
a=b;
b=tmp;
}
void heap_update(int i){
int bigger=i;
int l=(i<<)+;
int r=(i<<)+;
if(l<heap_size && heap[l]>heap[i])
bigger=l;
if(r<heap_size && heap[r]>heap[bigger])
bigger=r;
if(bigger!=i){
swaps(heap[i],heap[bigger]);
heap_update(bigger);
}
}
void heap_pop(){
if(heap_size>=){
swaps(heap[],heap[heap_size-]); //take the max to the rear.
heap_size--;
heap_update();
}
} int main()
{
int n;
int num[maxn],num2[maxn]; scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&num[i]);
}
for(int i=;i<n;i++){
scanf("%d",&num2[i]);
}
int idx;
for(idx=;idx<n- && num2[idx]<=num2[idx+];idx++);
int p=idx+;
for(;p<n && num[p]==num2[p];p++);
if(p==n){
sort(num2,num2+idx+); //相当于将num[idx+1]插入到前面排序
printf("Insertion Sort\n");
for(int i=;i<n-;i++)
printf("%d ",num2[i]);
printf("%d",num2[n-]);
}
else{
int sortedsize=;
int tmpnum[maxn];
for(int i=;i<n;i++)
tmpnum[i]=num[i];
sort(tmpnum,tmpnum+n);
p=n-;
/*
看了别人网上有写第三行AC的,
但按道理来说,如果样例2的第二个序列是6 4 5 0 1 2 3 7 8 9,那明显第三行就不对额。 10
3 1 2 8 7 5 9 4 6 0
6 4 5 0 1 2 3 7 8 9
Heap Sort
5 4 3 0 1 2 6 7 8 9 (第一行的输出)
5 4 0 6 1 2 3 7 8 9 (第三行的输出)
*/
for(;p>= && num2[p]==tmpnum[p];p--);
//for(;p>=1 && num2[p]>=num2[0];p--);
//for(;p>=1 && num2[p]>=num2[p-1];p--); //个人认为应该是过不了的。。。但却AC了 heap_size=p+;
for(int i=;i<n;i++)
heap[i]=num2[i];
heap_pop();
printf("Heap Sort\n");
for(int i=;i<n-;i++){
printf("%d ",heap[i]);
}
printf("%d",heap[n-]);
}
return ;
}

PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)的更多相关文章

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

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

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

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

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

  4. 【PAT甲级】1098 Insertion or Heap Sort (25 分)

    题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...

  5. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

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

  6. 1098. Insertion or Heap Sort (25)

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

  7. 1098 Insertion or Heap Sort (25分)

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

  8. PAT甲题题解-1002. A+B for Polynomials (25)-多项式相加

    注意两点:1.系数也有可能加起来为负!!!一开始我if里面判断为>0导致有样例没过...2.如果最后所有指数的系数都为0,输出一个0即可,原本以为是输出 1 0 0.0... #include ...

  9. PAT甲题题解-1039. Course List for Student (25)-建立映射+vector

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789157.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. yum安装某个包出现冲突的情况

    yum安装是非常方便的,可以自动解决依赖问题,但是有时候我们安装包会出现冲突,这个时候我们就要查找是哪些包与哪些包出现冲突,然后再针对性的解决问题. 一般来说起冲突的包会报出来,主要为两点 1.包与包 ...

  2. 某某D的手伸的实在太长了,路由器也未能幸免,致被阉割的TP-Link

    前段时间整了个服务器架上l2tp.server, TP-Link路由连上去后,全网走l2tp通道,而且不能配置相关的路由表 然后研究啊 找啊 查啊,确定是路由没有这功能 找客服问了一下,他一听就懂了, ...

  3. Springboot整合log4j2日志全解

    目录 常用日志框架 日志门面slf4j 为什么选用log4j2 整合步骤 引入Jar包 配置文件 配置文件模版 配置参数简介 Log4j2配置详解 简单使用 使用lombok工具简化创建Logger类 ...

  4. 【Ansible 文档】【译文】配置文件

    这里说明一下配置文件的内容,原文地址:http://docs.ansible.com/ansible/latest/intro_configuration.html 这个与[Ansible 文档]配置 ...

  5. 【SDOI2011 第2轮 DAY1】消防 -[树的直径+树链剖分][解题报告]

    [SDOI2011 第2轮 DAY1]消防 题面: SDOI2011 第2轮 DAY1]消防 时间限制 : 20000 MS 空间限制 : 565536 KB 问题描述 时限\(2s\) 某个国家有\ ...

  6. Nowcoder 提高组练习赛-R1

    https://www.nowcoder.com/acm/contest/172#question 单人报名300元,五人合报免费,于是就和学弟同学学长们组了一个三世同堂的队伍,高一的学长wzhqwq ...

  7. android studio InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annota

    如果 你的项目中使用了注解插件 比如butterknife   升级3.1之后打包编译  出现以下错误提示 InnerClass annotations are missing correspondi ...

  8. 使用docker搭建hadoop环境,并配置伪分布式模式

    docker 1.下载docker镜像 docker pull registry.cn-hangzhou.aliyuncs.com/kaibb/hadoop:latest 注:此镜像为阿里云个人上传镜 ...

  9. Docker技术入门与实战 第二版-学习笔记-8-网络功能network-1-单个host上的容器网络

    Docker 中的网络功能介绍 Docker 允许通过外部访问容器或容器互联的方式来提供网络服务 1) 外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -p或 -P参数 ...

  10. FileUriExposedException_Android7.0适配

    一. FileUriExposedException的解决 问题 由于在Android7.0上,google使用了新的权限机制,所以导致在调用相机的时候,如果传递的URI为”file://”类型,的系 ...