zoukankan      html  css  js  c++  java
  • pat1098. Insertion or Heap Sort (25)

    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
    

    提交代码

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

     1 #include<cstdio>
     2 #include<stack>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 using namespace std;
     9 int line1[105],line2[105];
    10 int main(){
    11     //freopen("D:\INPUT.txt","r",stdin);
    12     int n;
    13     scanf("%d",&n);
    14     int i,j,k,num;
    15     for(i=0;i<n;i++){
    16         scanf("%d",&line1[i]);
    17     }
    18     for(i=0;i<n;i++){
    19         scanf("%d",&line2[i]);
    20     }
    21     for(i=1;i<n;i++){
    22         int temp=line1[i];
    23         for(j=i-1;j>=0&&line1[j]>temp;j--){
    24             line1[j+1]=line1[j];
    25         }
    26         line1[j+1]=temp;
    27         for(k=0;k<n;k++){
    28             if(line1[k]!=line2[k]){
    29                 break;
    30             }
    31         }
    32         if(k==n){
    33             break;
    34         }
    35     }
    36     if(i<n){//继续插入排序
    37         printf("Insertion Sort
    ");
    38         i++;
    39         int temp=line1[i];
    40         for(j=i-1;j>=0&&line2[j]>temp;j--){
    41             line2[j+1]=line2[j];
    42         }
    43         line2[j+1]=temp;
    44     }
    45     else{
    46         printf("Heap Sort
    ");
    47         for(i=n-1;i>0&&line2[(i-1)/2]<line2[i];i--);
    48         int amount=i;//个数
    49         int temp=line2[i];
    50         line2[i]=line2[0];
    51         for(i=1;i<amount;i=i*2+1){
    52             if(i+1<amount&&line2[i+1]>line2[i]){
    53                 i++;
    54             }
    55             if(temp<line2[i]){
    56                 line2[(i-1)/2]=line2[i];
    57             }
    58             else{
    59                 break;
    60             }
    61         }
    62         line2[(i-1)/2]=temp;
    63     }
    64     printf("%d",line2[0]);
    65     for(i=1;i<n;i++){
    66         printf(" %d",line2[i]);
    67     }
    68     printf("
    ");
    69     return 0;
    70 }
  • 相关阅读:
    小程序开发-Canvas画布组件
    小程序开发-Map地图组件
    小程序开发-媒体组件video使用入门
    小程序开发-媒体组件image
    小程序开发-组件navigator导航篇
    小程序开发-表单组件的使用
    小程序开发-视图容器入门
    小程序开发-基础组件icon/text/progress入门
    小程序开发-block组件的使用
    小程序开发-小程序tabBar不显示的原因分析
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4781282.html
Copyright © 2011-2022 走看看