1098. Insertion or Heap Sort (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 N numbers. 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.

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort
5 4 3 1 0 2 6 7 8 9

提交代码

思路:先将原序列按插入排序排序,不断与第二序列比较,如果有相等,说明这是插入排序,否则,是堆排序。然后按排序操作进行下一步。

 #include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
using namespace std;
int line1[],line2[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j,k,num;
for(i=;i<n;i++){
scanf("%d",&line1[i]);
}
for(i=;i<n;i++){
scanf("%d",&line2[i]);
}
for(i=;i<n;i++){
int temp=line1[i];
for(j=i-;j>=&&line1[j]>temp;j--){
line1[j+]=line1[j];
}
line1[j+]=temp;
for(k=;k<n;k++){
if(line1[k]!=line2[k]){
break;
}
}
if(k==n){
break;
}
}
if(i<n){//继续插入排序
printf("Insertion Sort\n");
i++;
int temp=line1[i];
for(j=i-;j>=&&line2[j]>temp;j--){
line2[j+]=line2[j];
}
line2[j+]=temp;
}
else{
printf("Heap Sort\n");
for(i=n-;i>&&line2[(i-)/]<line2[i];i--);
int amount=i;//个数
int temp=line2[i];
line2[i]=line2[];
for(i=;i<amount;i=i*+){
if(i+<amount&&line2[i+]>line2[i]){
i++;
}
if(temp<line2[i]){
line2[(i-)/]=line2[i];
}
else{
break;
}
}
line2[(i-)/]=temp;
}
printf("%d",line2[]);
for(i=;i<n;i++){
printf(" %d",line2[i]);
}
printf("\n");
return ;
}

pat1098. Insertion or Heap Sort (25)的更多相关文章

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

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

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

  3. 1098. Insertion or Heap Sort (25)

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

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

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

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

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

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

  7. 09-排序3 Insertion or Heap Sort (25 分)

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

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

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

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

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

随机推荐

  1. 三、kafka主要配置

      1.Broker配置 <ignore_js_op>      2.Consumer主要配置 <ignore_js_op>  3.Producer主要配置 <ignor ...

  2. ViewController lifecyle(IOS学习)

    斯坦福的ios教学视频笔记一张,如下

  3. [hdu1712]ACboy needs your help分组背包

    题意:一共$m$天,$n$门课程,每门课程花费$i$天得到$j$的价值,求最后获得的最大价值 解题关键:分组背包练习,注意循环的顺序不能颠倒 伪代码: $for$ 所有的组$k$   $for{\rm ...

  4. layui 表格新增删除一行

    1.html 代码 <div class="layui-row layui-col-space5"> <div> <span style=" ...

  5. ubuntu下hive-0.8.1配置

    1.下载hive包wget http://labs.mop.com/apache-mirror/hive/stable/hive-0.8.1.tar.gz,并用tar -xzvf 将其解压到要安装的目 ...

  6. CF1042E Vasya and Magic Matrix

    感觉不会期望. 首先把所有格子按照权值从小到大排一下序,这样一共有$n * m$个元素,每个元素有三个属性$x, y, val$. 下文中的下标均为排序后的下标. 这样子我们就可以推出公式: $f_i ...

  7. POJ 2398 Toy Storage (叉积判断点和线段的关系)

    题目链接 Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4104   Accepted: 2433 ...

  8. 删除docker私有仓库中的镜像

    1.搭建私有仓库 (1)拉取私有仓库镜像 docker pull registry(2)启动私有仓库容器 docker run ‐di ‐‐name=registry ‐p 5000:5000 reg ...

  9. Ping++中的AlipaySDK和AlicloudUTDID冲突解决方案

    今天维护一个老项目发现阿里框架冲突 问题截图: 解决方案: 去阿里文档中心 https://docs.open.alipay.com/54/104509 重新下载没有UTDID冲突的库 下载SDK解压 ...

  10. web.config中authorization下的location中的path的设置 (转)

    项目下 有三个文件夹 A,B,C 验正方式是 Forms 验正 我要设置他们的访问权限为, A,匿名可访问 B,普通用户授权后才能访问 C,只允许管理员访问 <configuration> ...