• 排序应用举例


    问题描述:

    在给定结合S中,是否存在之和与给定数x相等的两数,有则输出。

    #include<iostream>
    using namespace std;
    const int maxn = 5000;
    int main()
    {
        void merge_sort(int a[], int beg, int end);
        void put(int a[], int n);
        int binary_sort(int A[], int y, int beg, int end);
        int a[maxn], x, n;
        cout << "Enter two numbers: ";
        cin >> x >> n;
        cout << "Enter some numbers:" << endl;
        for(int i = 0; i < n ; ++i)
            cin >> a[i];
        merge_sort(a, 0, n-1);
        int index = 0;
        for(int i = 0; i < n; ++i)
        {
            index = binary_sort(a, x-a[i], 0, n-1);
            if(index != -1 && index != i)
                cout << "YES!!!     "<< a[i] << " + " <<a[index] << " == " << x << endl;
        }
        if(index == -1)
            cout << "NO!!!" << endl;
        put(a, n);
        return 0;
    }
    
    void merge(int a[], int beg, int mid, int end)
    {
        //合并
        int n1 = mid - beg + 1, n2 = end - mid;
        int L[n1+1], R[n2+1];
        for(int i = 0; i < n1; ++i)
            L[i] = a[beg+i];
        L[n1] = 50000;   //哨兵元素
        mid = mid + 1;
        for(int i = 0; i < n2; ++i)
            R[i] = a[mid+i];
        R[n2] = 50000;   //哨兵元素
        int i=0, j =0, k = beg;
        //------------------块一----------------------------------
        for(int k = beg; k <= end; ++k)
        {
            //当同时遇到哨兵意味着所有牌已经合并
            if(L[i] <= R[j])
            {
                a[k] = L[i];
                ++i;
            }
            else
            {
                a[k] = R[j];
                ++j;
            }
        }
        //------------------块一----------------------------------
    }
    
    void merge_sort(int a[], int beg, int end)
    {
        //将n个元素分成各个汗n/2个元素的子问题
        int mid = (beg+end)/2;
        if(beg < end)
        {
            merge_sort(a, beg, mid);
            merge_sort(a, mid+1, end);
            merge(a, beg, mid, end);
        }
    }
    
    void put(int a[], int n)
    {
        for(int i = 0; i < n; ++i)
            cout << a[i] << "  ";
        cout << endl;
    }
    
    int binary_sort(int a[], int y, int beg, int end)
    {//二分查找
        int mid;
        while(beg < end)
        {
            mid = (beg+end)/2;
            if(y == a[mid])
                return mid;
            if(y > a[mid])
                beg = mid + 1;
            else
                end = mid -1;
        }
        return -1;
    }
    
    //时间复杂度T(n) = Θ(nlgn);
    /*
    
    8   8
    1 2 6 4 7 9 3 5
    
    */
    

      

  • 相关阅读:
    Luogu P4053 [JSOI2007]建筑抢修
    CF894E Ralph and Mushrooms
    Boruvka
    Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland
    HDU 3714/UVA1476 Error Curves
    HDU 5410 CRB and His Birthday
    HDU 1796 How many integers can you find
    UVA 11624 Fire!
    POJ 3279 Dungeon Master
    POJ 1321 棋盘问题
  • 原文地址:https://www.cnblogs.com/sanghai/p/2809334.html
Copyright © 2020-2023  润新知