PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列。
插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同。
堆排序的特点就是,后面是从小到大排列的最大的几个数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)-(插入排序和堆排序)的更多相关文章
- 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 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT (Advanced Level) 1098. Insertion or Heap Sort (25)
简单题.判断一下是插排还是堆排. #include<cstdio> #include<cstring> #include<cmath> #include<ve ...
- 【PAT甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 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 ...
- 1098 Insertion or Heap Sort (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT甲题题解-1002. A+B for Polynomials (25)-多项式相加
注意两点:1.系数也有可能加起来为负!!!一开始我if里面判断为>0导致有样例没过...2.如果最后所有指数的系数都为0,输出一个0即可,原本以为是输出 1 0 0.0... #include ...
- PAT甲题题解-1039. Course List for Student (25)-建立映射+vector
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789157.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- Hadoop YARN简介
背景 本文整理一些Hadoop YARN的相关内容. 简介 YARN(Yet Another Resource Negotiator)是Hadoop通用资源管理平台,为各类计算框架(离线MR.在线St ...
- Android Bitmap Drawable byte[] InputStream 相互转换方法
用android处理图片的时候,由于得到的资源不一样,所以经常要在各种格式中相互转化,以下介绍了 Bitmap Drawable byte[] InputStream 之间的转换方法: import ...
- apache中的directory 和virtualhost有啥区别和联系呀
directory是virtualhost里的一项.用来指定目录访问权限与方法 . alias用来指定URL中的路径.第二项是物理路径,与directory完全相同才成. location有些象是将a ...
- Scala学习之路 (六)Scala的类、对象、继承、特质
一.类 1.类的定义 scala语言中没有static成员存在,但是scala允许以某种方式去使用static成员这个就是伴生机制,所谓伴生,就是在语言层面上,把static成员和非static成员用 ...
- C/C++常用库及工具
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在Linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...
- 在openresty或nginx编译nginx-upsync-module&nginx_upstream_check_module
针对我在编译在两个模块的过程中遇到的一系列问题,特此记录编译流程的一些细节 1.下载 install git git clone https://github.com/weibocom/nginx-u ...
- Python2.7-hmac
hmac 模块,基于密钥的哈希算法 1.模块对象 1.1 HMAC 对象 1.1.1 初始化构建类:需要通过模块方法 hmac.new(key[, msg[, digestmod]]) 创建一个新对象 ...
- CSS中的before和:after伪元素深入理解
1.定义: “伪元素”,顾名思义.就是它创建了一个虚假的元素,并且将其虚假的元素插入到目标元素的内容之前或之后. 2:特点: a.它在实际文档中不改变什么,但是对用户可见,可以通过css控制,源码中看 ...
- Windows控制程序网站带宽及Qos(TOS或DSCP)
[基于策略的 Qos]位置:gpedit.msc->本地计算机策略->用户配置->Windows 设置->基于策略的 Qos
- Python3入门(十)——调试与测试
一.异常处理 1.try...except...finally... 这个也就是Java里的try...cath..finally...了,直接看经典代码: try: print("开始执行 ...