进阶实验1-3.1 两个有序序列的中位数 (25 分)

已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A0, A1,…, AN-1 的中位数指 A(N−1)/2 的值,即第 ⌊(N+1)/2⌋ 个数(A0为第1个数)。


输入格式:

输入分三行。第一行给出序列的公共长度N(0<N≤100000),随后每行输入一个序列的信息,即N个非降序排列的整数。数字用空格间隔。

输出格式:

在一行中输出两个输入序列的并集序列的中位数。

输入样例1:

5
1 3 5 7 9
2 3 4 5 6
结尾无空行

输出样例1:

4
结尾无空行

输入样例2:

6
-100 -10 1 1 1 1
-50 0 2 3 4 5

输出样例2:

1

AC:

  • V e c t o r
#include <bits/stdc++.h>
using namespace std;

int main()
{
    std::ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> s;
    for (int i = 0; i < 2 * n;i++)
    {
        int a;
        cin >> a;
        s.push_back(a);
    }
    sort(s.begin(), s.end());
    cout << s[(s.size() - 1) / 2] << endl;
    return 0;
}
  • 这里提供下实验书上的一个思路:
    由于原序列有序,可以借鉴二分法
    假设 S1 为 A0, A1, …, AN-1 ,
    S2 为 B0, B1, …, BN-1,
    其中 S1中位数为 A(N-1)/2 ,S2中位数为B(N-1)/2 .
  • 若记最终并集的中位数为 M,
    则可证明 min(A(N-1)/2,B(N-1)/2)<=M<=max(A(N-1)/2,B(N-1)/2).
    因此,当 N为1时,中位数 M=min(A0,B0)。
    一般情况下,若两个中位数相等,即 A(N-1)/2==B(N-1)/2,
    则 M=A(N-1)/2 ;
    若不相等,即 A(N-1)/2 != B(N-1)/2 ,
    不妨假设 A(N-1)/2 < B(N-1)/2,
    可证明,序列 S1从 A(N-1)/2 左面去除 k个数,
    同时序列 S2从 B(N-1)/2 右面去除 k个数后,
    新序列并集的中位数仍然为 M。
    因此,可转换为求两个新序列的中位数。

WA:

 #include <bits/stdc++.h>
using namespace std;
int main()
{
    std::ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int *A = new int();
    int *B = new int();
    for (int i = 0; i < n; i++)
        cin >> A[i];
    for (int j = 0; j < n; j++)
        cin >> B[j];
    int as = 0, ae = n - 1;
    int bs = 0, be = n - 1;
    int am, bm, k;
    //s表示起点start,e表示终点end
    while (n > 1)
    {
        am = (as + ae) / 2;
        bm = (bs + be) / 2;
        //寻找中位数
        if (n != 2)
            k = (n - 1) / 2;
        else
            k = n / 2;
        if (A[am] == B[bm])
        {
            cout << A[am] << endl;
            return 0;
        }
        else if (A[am] < B[bm])
        {
            as += k;
            be -= k;
        }
        else
        {
            ae -= k;
            bs += k;
        }
        n = n - k;
        //迭代求 A,B数组中剩下的最后一位数
    }
    if(A[ae] < B[be])
        cout << A[ae] << endl;
    if(A[ae] > B[be])
        cout << B[be] << endl;
    delete A;
    delete B;
    return 0;
}

感觉也没什么问题…
样例检测无误

  • 结果
    不清楚为什么
    改了半天,还是不对!
    难道是c++ 中的 new 与c 中 的 malloc 区别?
    试了一下,果真 !

AC:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    std::ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    int* A = (int*)malloc(n * sizeof(int)); 
    int* B = (int*)malloc(n * sizeof(int));
    for (int i = 0; i < n; i++)
        cin >> A[i];
    for (int j = 0; j < n; j++)
        cin >> B[j];
    int as = 0, ae = n - 1;
    int bs = 0, be = n - 1;
    int am, bm, k;
    //s表示起点start,e表示终点end
    while (n > 1)
    {
        am = (as + ae) / 2;
        bm = (bs + be) / 2;
        //寻找中位数
        if (n != 2)
            k = (n - 1) / 2;
        else
            k = n / 2;
        if (A[am] == B[bm])
        {
            cout << A[am] << endl;
            return 0;
        }
        else if (A[am] < B[bm])
        {
            as += k;
            be -= k;
        }
        else
        {
            ae -= k;
            bs += k;
        }
        n = n - k;
        //迭代求 A,B数组中剩下的最后一位数
    }
    if(A[ae] < B[be])
        cout << A[ae] << endl;
    if(A[ae] > B[be])
        cout << B[be] << endl;
    free(A);
    free(B);
    return 0;
}

听闻 c++ 中有个迭代器。
不会用!ಥ_ಥ

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值