7-7 两个有序序列的中位数 (300分)

7-7 两个有序序列的中位数 (300分)

已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A0,A1​​ ,⋯,AN−1的中位数指A(N−1)/2的值,即⌊(N+1)/2个数(A​0为第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

代码

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char const *argv[])
{
    vector<int> v1;
    vector<int> v2;

    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v1.push_back(num);
    }
    
    // for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
    // {
    //     cout << *it << endl;
    // }
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        v2.push_back(num);
    }

    v2.insert(v2.end(), v1.begin(), v1.end());

    sort(v2.begin(), v2.end());
    // for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
    // {
    //     cout << *it << endl;
    // }

    int len = v2.size();
    int m = (len+1)/2;
    //cout << m<< endl;

    cout << v2.at(m-1) << endl;
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值