zoukankan      html  css  js  c++  java
  • PAT 1089. Insert or Merge (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.

    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
    /*
     * 这题给了200ms的时间  n<=100 所以这题即使每排序一趟与中间序列进行比较  时间复杂度也仅为O(n^2) 完全可以
     * 还有个O(n)的算法:
      1. 插入排序特点为  前面均为有序序列  没排序的与原序列相同  
      2. 归并排序进行了几趟归并  可通过len=2 到 n/2 进行枚举  直到不满足每一组归并序列都有序的条件时  即可得到当前
      进行了几次归并
    
      另外 这题题目好像有点问题  比如 1 2 4 3 中间序列为 1 2 4 3 你根本不知道是进行了2趟还是3趟插入排序@—-@。 
    */
    #include "iostream"
    #include "algorithm"
    using namespace std;
    void merge(int a[], int temp[], int left, int right, int rightEnd) { /* 合并2个有序的子序列 */
        int l = left;
        int leftEnd = right - 1;
        int len = rightEnd - left + 1;
        while (left <= leftEnd && right <= rightEnd) {
            if (a[left] <= a[right]) {
                temp[l] = a[left];
                left++;
            }
            else {
                temp[l] = a[right];
                right++;
            }
            l++;
        }
        while (left <= leftEnd)
            temp[l++] = a[left++];
        while (right <= rightEnd)
            temp[l++] = a[right++];
    }
    /* 一趟2路归并 */
    void MergePass(int a[], int temp[], int n, int length) { /* length:当前有序子列的长度 */
        int i;
        for (i = 0; i <= n - 2 * length; i += 2 * length) {
            merge(a, temp, i, i + length, i + 2 * length - 1);
        }
        if (i + length < n)
            merge(a, temp, i, i + length, n - 1);
        else
            for (int j = i; j < n; j++)
                temp[j] = a[j];
    }
    int getLen(int b[], int n) {   /* 进行一趟归并排序 */
        for (int len = 2; len <= n / 2; len *= 2) {     
            int i;
            for (i = 0; i <= n - 2 * len; i += 2 * len)
                if (!is_sorted(b + i, b + i + 2 * len)) return len;
            if (!is_sorted(b + i, b + n)) return len;
        }
    }
    bool judge(int a[],int b[],int n) { /* 判断是不是插入排序 */
        int len = 1;
        int i;
        for ( i = 0; i < n - 1; i++)
            if (b[i] > b[i + 1]) {
                len = i + 1;
                break;
            }
        for (i=len; i < n; i++) {
            if (a[i] != b[i])
                return false;
        }
        sort(b, b + len + 1);
        return true;
    }
    void print(int a[], int n) {
        for (int i = 0; i < n; i++) {
            if (i == 0)
                cout << a[i];
            else
                cout << " " << a[i];
        }
        cout << endl;
    }
    int main() {
        int a[101], b[101];
        int n;
        cin >> n;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        for (int i = 0; i < n; i++) {
            cin >> b[i];
        }
        if (judge(a, b, n)) {
            cout << "Insertion Sort" << endl;
            print(b, n);
        }
        else {
            cout << "Merge Sort" << endl;
            int* temp = (int *)malloc(n * sizeof(int));
            if (temp != NULL) {
                MergePass(b, temp, n, getLen(b, n));
                print(temp,n);
                free(temp);
            }
        }
    }
  • 相关阅读:
    codefoeces problem 671D——贪心+启发式合并+平衡树
    bzoj 1598: [Usaco2008 Mar]牛跑步
    bzoj 1050: [HAOI2006]旅行comf&&【codevs1001】
    codefoeces 671 problem D
    利用FFMPEG以及EasyRTMP实现读取H.264文件推RTMP视频流的两种方式
    视频流拉转推工具对比:EasyRTSPLive和FFMPEG拉转推效果对比
    TSINGSEE青犀视频Webrtc实时通信的构建流程:PeerConnection对等通信的实现方式
    TSINGSEE青犀视频云边端架构产品编译Intel Media SDK 编译报错error"SSE4.1 instruction set not enabled"
    H.265编码视频在web网页实现无插件播放,应该通过软解码还是硬解码?
    【案例分析】EasyDSS+EasyCVR融合共享解决方案
  • 原文地址:https://www.cnblogs.com/minesweeper/p/6152965.html
Copyright © 2011-2022 走看看