zoukankan      html  css  js  c++  java
  • pat1089. Insert or Merge (25)

    1089. Insert or Merge (25)

    时间限制
    200 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.

    Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

    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 "Merge 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 0 6
    1 3 2 8 5 7 4 9 0 6
    
    Sample Output 2:
    Merge Sort
    1 2 3 8 4 5 7 9 0 6
    

    提交代码

    这里的插入排序和非递归的归并,要注意。

      1 #include<cstdio>
      2 #include<cmath>
      3 #include<cstring>
      4 #include<stack>
      5 #include<algorithm>
      6 #include<iostream>
      7 #include<stack>
      8 #include<set>
      9 #include<map>
     10 #include<vector>
     11 #include<queue>
     12 using namespace std;
     13 int line[105],order[105],line1[105];
     14 /*void HeapSort(int *order,int n){
     15     int i=n-1;
     16     while(order[i]>order[(i-1)/2]){
     17         i--;
     18     }
     19     int temp=order[i];
     20     order[i]=order[0];
     21     int j=1;//最小左节点
     22     for(;j<i;j=2*j+1){
     23         if(j+1<i&&order[j+1]>order[j]){
     24             j++;
     25         }
     26         if(order[j]>temp){
     27             order[(j-1)/2]=order[j];
     28         }
     29     }
     30     order[(j-1)/2]=temp;
     31 }*/
     32 void Merge(int order[],int temp[],int l,int r,int e){
     33     int i=l,j=r,p=l;
     34     while(i<r&&j<=e){
     35         if(order[i]<order[j]){
     36             temp[p++]=order[i++];
     37         }
     38         else{
     39             temp[p++]=order[j++];
     40         }
     41     }
     42     while(i<r){
     43         temp[p++]=order[i++];
     44     }
     45     while(j<=e){
     46         temp[p++]=order[j++];
     47     }
     48 }
     49 void Merge_pass(int order[],int temp[],int n,int len){
     50     int i;
     51     for(i=0;i<=n-2*len;i+=2*len){
     52         Merge(order,temp,i,i+len,i+2*len-1);
     53     }
     54     if(i+len<n){
     55         Merge(order,temp,i,i+len,n-1);//Merge(i,i+len,n-1);
     56     }
     57     else{
     58         while(i<n){
     59             temp[i]=order[i];
     60             i++;
     61         }
     62     }
     63 }
     64 void MergeSort(int *line,int *order,int n){
     65     int *temp=new int[n+1];
     66     int len=1,i;
     67     while(len<n){
     68         Merge_pass(line,temp,n,len);
     69         for(i=0;i<n;i++){
     70             line[i]=temp[i];
     71             //cout<<i<<" "<<line[i]<<endl;
     72         }
     73         for(i=0;i<n;i++){
     74             if(line[i]!=order[i]){
     75                 break;
     76             }
     77         }
     78         if(i==n){
     79             break;
     80         }
     81         len*=2;
     82     }
     83     if(len<n){
     84         Merge_pass(temp,order,n,len*2);
     85         /*for(i=0;i<n;i++){
     86             cout<<i<<" "<<order[i]<<endl;
     87         }*/
     88     }
     89     delete []temp;
     90 }
     91 int main()
     92 {
     93     //freopen("D:\INPUT.txt","r",stdin);
     94     int n;
     95     scanf("%d",&n);
     96     int i,j;
     97     for(i=0;i<n;i++){
     98         scanf("%d",&line[i]);
     99         line1[i]=line[i];
    100     }
    101     for(i=0;i<n;i++){
    102         scanf("%d",&order[i]);
    103     }
    104     for(i=1;i<n;i++){
    105         int temp=line[i];
    106         for(j=i;j>=1&&line[j-1]>temp;j--){
    107             line[j]=line[j-1];
    108         }
    109         line[j]=temp;
    110         for(j=0;j<n;j++){
    111             if(line[j]!=order[j]){
    112                 break;
    113             }
    114         }
    115         if(j==n){
    116             break;
    117         }
    118     }
    119     if(i==n){//merge sort
    120         printf("Merge Sort
    ");
    121         MergeSort(line1,order,n);
    122     }
    123     else{//insertion sort
    124         printf("Insertion Sort
    ");
    125         i++;
    126         int temp=order[i];
    127         for(j=i;j>=1&&order[j-1]>temp;j--){//i一定要指向最后
    128             order[j]=order[j-1];
    129         }
    130         order[j]=temp;
    131     }
    132     printf("%d",order[0]);
    133     for(i=1;i<n;i++){
    134         printf(" %d",order[i]);
    135     }
    136     printf("
    ");
    137     return 0;
    138 }
  • 相关阅读:
    [转]Java中fina以及static的意义
    [转]Java中this的意义
    [转]Java中子类调用父类构造方法的问题分析
    [原创]SSH中HibernateTemplate与HibernateDaoSupport关系
    [转]No configuration found for the specified action解决办法
    [原创]MyEclipse2014全手动实现反向工程---解决手动整合ssh时发生的、在hibernate反向工程的时候找不到项目名的问题
    [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
    pycharm中运行时添加配置 及pytest模式怎么修改为run模式
    字符串正则匹配替换
    PyCharm选中文件夹新建时Directory与Python package的区别
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4787452.html
Copyright © 2011-2022 走看看