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 ...
随机推荐
- ruby中特殊的全局变量
全局变量:由$开头,可以在程序的任何位置访问到.在初始化前,全局变量有一个特殊的值 nil. 这里列出了一些以$打头并跟单个字符的特殊变量,包括主要的系统变量以及它们的含义: $! 最近一次的错误信息 ...
- Centos7搭建pptp
废话不多说,先上脚本地址:Centos7一键pptp 使用方法: wget https://raw.githubusercontent.com/DanylZhang/VPS/master/CentOS ...
- J2EE 工作中注意事项
[转载于http://www.cnblogs.com/hemingwang0902/archive/2012/01/06/2314215.html] 根据当前项目中代码存在的一些问题,编写了一个编码注 ...
- Web Pages(单页面模型)
.NET 是一套框架,用来个HTML.JS.CSS和服务器端脚本构建网页和网站. 可以有三种开发模式:Web Pages(单页面模型).MVC(模型视图控制器).Web Forms(事件驱动模型) W ...
- [dp]编辑距离问题
https://www.51nod.com/tutorial/course.html#!courseId=3 转移方程: 注意如何对齐的. 这个算法的特点是,S和T字符串左边始终是对齐的.为了更好地理 ...
- .net开发ActiveX控件
我估计有些朋友不清楚ActiveX控件,但这篇博客不是来解释这些概念的.如果你对ActiveX的概念不清楚,请参考这里: http://baike.baidu.com/view/28141.htm 这 ...
- .net Core命令行,Json配置
创建.netCore控制台 NuGet :Microsoft.AspNetCore.All static void Main(string[] args) { var builder = new Co ...
- [CentOS7] 增加yum源
下载最新rpm文件:http://fedoraproject.org/wiki/EPEL 通过源文件rpm来增加: rpm -ivh epel-release-latest-7.noarch.rpm
- return die exit 常用
die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值die是遇到错误才停止exit是直接停止,并且不运行后续代码,exit()可以显示内容.return就是纯粹的返回 ...
- 牛客网NOIP赛前集训营-提高组(第四场)B题 区间
牛客网NOIP赛前集训营-提高组(第四场) 题目描述 给出一个序列 a1, ..., an. 定义一个区间 [l,r] 是好的,当且仅当这个区间中存在一个 i,使得 ai 恰好等于 al, al+1, ...