zoukankan      html  css  js  c++  java
  • 数据结构与算法题目集(中文)——5-53 两个有序序列的中位数 (25分)——链表

    已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A_0, A_1, cdots, A_{N-1}A0​​,A1​​,,AN1​​的中位数指A_{(N-1)/2}A(N1)/2​​的值,即第lfloor(N+1)/2 floor(N+1)/2⌋个数(A_0A0​​为第1个数)。

    输入格式:

    输入分三行。第一行给出序列的公共长度N(0<<Nle≤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


     
    • 时间限制:200ms
    • 内存限制:64MB
    • 代码长度限制:16kB
    • 判题程序:系统默认
    • 作者:DS课程组
    • 单位:浙江大学
    #include<iostream>
    using namespace std;
    int move(int*a,int*b,int n){
        while(--n){
            if(*a >= *b) b++;
            else a++;
        }
        return (*a)>(*b)?(*b):(*a);
    }
    int main(){
        int n;
        cin>>n;
        int* a = new int[n];
        int* b = new int[n];
        for(int i=0; i<n; i++ ){
            cin>>*(a+i);
        }
        for(int i=0; i<n; i++ ){
            cin>>*(b+i);
        }
        cout<<move(a,b,n)<<endl;
        return 0;
    }
     
    个人分享,欢迎指导,未经允许,请勿转载。谢谢!
  • 相关阅读:
    learnyou 相关网站
    hdu 3038 How Many Answers Are Wrong
    hdu 3047 Zjnu Stadium 并查集高级应用
    poj 1703 Find them, Catch them
    poj 1182 食物链 (带关系的并查集)
    hdu 1233 还是畅通工程
    hdu 1325 Is It A Tree?
    hdu 1856 More is better
    hdu 1272 小希的迷宫
    POJ – 2524 Ubiquitous Religions
  • 原文地址:https://www.cnblogs.com/hello-OK/p/7053180.html
Copyright © 2011-2022 走看看