PAT(甲级)1098
来源:互联网 发布:vscode代码格式化 编辑:程序博客网 时间:2024/06/06 13:11
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:103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0Sample Output 1:
Insertion Sort1 2 3 5 7 8 9 4 6 0Sample Input 2:
103 1 2 8 7 5 9 4 6 06 4 5 1 0 3 2 7 8 9Sample Output 2:
Heap Sort5 4 3 1 0 2 6 7 8 9
#include <iostream>#include <cstring>#define SIZE 105using namespace std;int in1[SIZE];int in2[SIZE];int out[SIZE];bool equal(int a[],int b[],int n){for(int i=0;i<n;i++) if(a[i] != b[i]) return false;return true;}bool Test_insertion(int in1[],int out[],int n){bool flag = false;for(int i=1;i<n;i++){int tmp = in1[i];int j;for(j= i-1;j>=0;j--){if(in1[j] > tmp) in1[j+1] = in1[j];else break;}in1[j+1] = tmp;if(flag){ return true;}if(equal(in1,out,n)) flag = true;}return flag;}void FilterDown(int a[],int pos,int end){int i = 2*pos+1;int tmp = a[pos];;while(i <= end){if(i < end && a[i] < a[i+1]) i++; if(tmp >= a[i]) break; else{ a[pos] = a[i]; pos =i; i = 2*pos+1;}}a[pos] = tmp;}void heap_remove(int a[],int end){int tmp = a[0];a[0] = a[end];end--;FilterDown(a,0,end);a[end+1] = tmp;}void build_heap(int a[],int end){int i = (end-1)/2;while(i >= 0){FilterDown(a,i,end);i--;} heap_remove(a,end);}bool Test_heap(int in2[],int out[],int n){bool flag = false;for(int i=0;i<n;i++){build_heap(in2,n-1-i);if(flag) return true;if(equal(in2,out,n)) flag =true;}return flag;}void display(int a[],int n){for(int i=0;i<n-1;i++) cout <<a[i] <<' ';cout <<a[n-1] <<endl;}int main(){int N;//freopen("test.txt","r",stdin);scanf("%d",&N);for(int i=0;i<N;i++){scanf("%d",&in1[i]);}for(int i=0;i<N;i++){scanf("%d",&out[i]);}memcpy(in2,in1,N*sizeof(int));if(Test_insertion(in1,out,N)){cout <<"Insertion Sort\n";display(in1,N);} else{Test_heap(in2,out,N);cout <<"Heap Sort\n";display(in2,N);} //fclose(stdin);return 0;}
- PAT(甲级)1098
- PAT甲级1098
- PAT 甲级
- **浙大PAT甲级 1098堆排序
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- PAT(甲级)1014
- LeetCode题解——Add Digits
- PAT(甲级)1097
- JSP解析JSONObject类型对象出现null object异常解决方案
- Mybatis系列(二)配置
- spring使用手动装载依赖的bean
- PAT(甲级)1098
- JS Map 和 List 的简单实现代码
- JavaScript:继承
- hdu1199 初学离散化的一点心得
- Activity的四种加载模式
- Ralink WIFI driver QBSS_LoadAlarm自动切换channel研究
- PAT(甲级)1099
- 黑马程序员学习(十七) java反射机制个人学习经验
- Egit 解决冲突