pat1098. Insertion or Heap Sort (25)
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 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)的更多相关文章
- pat 甲级 1098. Insertion or Heap Sort (25)
1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- 1098. Insertion or Heap Sort (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 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 ...
- 09-排序3 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 分)
题意: 输入一个正整数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 ...
随机推荐
- UDK编辑器 49条小提示
转自:http://www.cnblogs.com/hmxp8/archive/2012/02/09/2343674.html Very Helpful~ 01. First time using t ...
- UE3优化
转自:http://www.cnblogs.com/NEOCSL/p/3320510.html 优化问题有很多内容可讲,涉及林林总总.今天我总结一下优化注意的地方. 1.从AnimTree和Skele ...
- Python模块-chardet模块
chardet模块用来获取文件的编码 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import chardet f = o ...
- Python-编写一个mysql注入漏洞检测工具
判断mysql网站是否存在注入漏洞的几个方法: 注入点后加上一个单引号会报错 and 1=1返回正常页面,and 1=2返回的页面不同于正常页面 and sleep(3) 网页会等待3秒左右 根据返回 ...
- Python,PyCharm2017安装教程,包含注册码[转载]
一,安装PyCharm 1.下载PyCharm 进入https://www.jetbrains.com/pycharm/download/#section=windows官网下载页面,可以到到PyCh ...
- cygwin运行git submodule init出错error while loading shared libraries的解决
installing the Devel\gettext package should solve your problem. git-submodule requires that. Unfortu ...
- 转:Linux下用Jmeter做接口测试
本地设计 首先在本地设计 Apache JMeter 测试计划,大家可以参考<接口测试之 JMeter 初探> ,这里不再重复. 服务器配置 确保服务器已经安装了JDK和Python. 在 ...
- p4555&bzoj2565 最长双回文串
传送门(洛谷) 传送门(bzoj) 题目 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc的顺序为abc,逆序为cba,不相同). 输入长度为 nnn 的串 SSS ...
- PCC-S-02201, Encountered the symbol "DB_USER_OPER_COUNT"
今天编译PROC程序时,遇到这个错误.最后发现原因是.pc文件里声明变量块时,不识别结构体. 今天时间紧知识用第一种方法暂时解决了.晚上抽时间用第二种方法优化一下代码. 查了很多资料,发现只有这个答案 ...
- Makefiles
A tutorial by example Compiling your source code files can be tedious, specially when you want to in ...