PAT A1098 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 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 resulting 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 <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ;
int raw[],res[],tmp[];
bool cmp(int now[],int tar[]){
for(int i=;i<;i++){
if(now[i]!=tar[i]) return false;
}
return true;
}
int vis[maxn]={};
int main(){
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&raw[i]);
tmp[i]=raw[i];
}
for(int i=;i<n;i++){
scanf("%d",&res[i]);
}
int flag=,j;
for(j=;j<=n;j++){
sort(tmp,tmp+j);
if(cmp(tmp,res)) flag=;
if(flag==)break;
}
if(flag==){
printf("Insertion Sort\n");
sort(raw,raw+j+);
for(int i=;i<n;i++){
printf("%d",raw[i]);
if(i!=n-) printf(" ");
}
return ;
}
}
注意点:题目完全没说堆排序怎么实现的,对于我这种小白来说,完全不知道堆排序里发生了什么,考试也快到了,先放着吧,只通过插入排序也有11分。未完待续。。
PAT A1098 Insertion or Heap Sort (25 分)——堆排序和插入排序,未完待续。。的更多相关文章
- 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甲级】1098 Insertion or Heap Sort (25 分)
题意: 输入一个正整数N(<=100),接着输入两行N个数,表示原数组和经过一定次数排序后的数组.判断是经过插入排序还是堆排序并输出再次经过该排序后的数组(数据保证答案唯一). AAAAAcce ...
- 09-排序3 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 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- pat1098. 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)-(插入排序和堆排序)
题目就是给两个序列,第一个是排序前的,第二个是排序中的,判断它是采用插入排序还是堆排序,并且输出下一次操作后的序列. 插入排序的特点就是,前面是从小到大排列的,后面就与原序列相同. 堆排序的特点就是, ...
- 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 ...
随机推荐
- 【ajax】1、ajax原理
Ajax核心XMLHttpRequest对象 XMLHttpRequest对象方法 XMLHttpRequest对象属性 onreadystatechange 状态改变的事件触发器.每次 readyS ...
- API接口规范V1.0——制定好规范,才好合作开发
返回码规范: 统一六位 000000 表示成功! 参数相关返回码预留100000-199999:系统相关返回码预留200000-299999:数据中心310000-319999后续项目以此类推,后续根 ...
- returnFunc.js
var cyn = function(){ console.log("one") return function(){ console.log("two") } ...
- Android--实现ViewPager边界回弹效果(转)
该View转自 http://blog.csdn.net/Kalwang/article/details/4708721 ,感谢这位大神. public class BounceBackView ...
- 安卓preview不显示的问题
Render Problem Failed to load AppCompat ActionBar with unknown error 解决方法:将styles.xml文件中的: <resou ...
- 简单整理关于C#和Java的区别
相信每个程序猿都有自己最喜欢的编程语言,然而对于编程语言似乎形成一条独特的鄙视链,就如Java和C#常常两边的开发者都是相互鄙视,然后他们一起共同鄙视全世界最好的编程语言——PHP 哈哈,但是其实我想 ...
- IntelliJ idea 如何打开左边项目展开栏
vie->Tool Windows->Project Alt+1 转自:https://blog.csdn.net/bug_moving/article/details/53284434
- Python 列表(List)包含的函数与方法
Python列表函数&方法 Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表 ...
- phoneGap使用 (MAC)
一.安装 ①先安装NodeJS(如果有的就不用安装了) http://nodejs.org/ ②.sudo npm install -g phonegap 需要等待安装完成 ③.检测是否安装成功 no ...
- Django view 视图
request.method 判断请求方式 8种 GET : 获取一个页面 POST: 提交数据 PUT : 上传 HEAD: 不用上传就获取数据 DELETE: 删除 Request-URL 标识的 ...