• 1029 Median


    Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.

    Given two increasing sequences of integers, you are asked to find their median.

    Input Specification:

    Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.

    Output Specification:

    For each test case you should output the median of the two given sequences in a line.

    Sample Input:

    4 11 12 13 14
    5 9 10 15 16 17
    

    Sample Output:

    13
    //题解:
    //     1.合并排序 ,超时超内存(舍)
    //     2.维护 两个队列,比较对头大小,循环出队:原先的测试点可以通过的,
    //      后来新版PAT加了内存限制,超内存(舍)
    //     3.在2 的基础上优化,第一个队列存好后,把第二个队列边读,边和第一个队列比较,选择出队。
    //      这样可以不用一次存完第二个队列,解决超内存的问题。
    //思路 :
    //    第一 、二个序列分别有n, m个元素,所以需要从队头剔除(n + m - 1) / 2个元素,
    //      最后答案就是两个队头的最小值。
    //   对于最终答案在第一第二个队列中的情况要分开处理。若答案在第二个队列中,
    //    在输入数据时就可以提前得出答案并退出,若答案在第一个队列中,要二次出队才能找到答案。
    //
    //注意:
    //    在所有元素入队列完毕后,把INT_MAX入队列,一是这样队列永不为空,方便处理。
    //    二是,题目的long int因为内存限制原因,并不会为最终答案,只
    //    是干扰数据,所以每次遇到这样的干扰数据把他设为INT_MAX就好了。
    #include<cstdio>
    #include<queue>
    using namespace std;
    const int INF = 10000000;
    int main(){
        queue<int> a,b;
        int n,m,x,count = 0;
        scanf("%d",&n);
        for(int i = 0 ; i < n ; i++){
            scanf("%d",&x);
            a.push(x);
        }
        a.push(INF);
        scanf("%d",&m);
        for(int i = 0 ; i < m ; i++){
            scanf("%d",&x);
            b.push(x);
            if(count == (n+m-1)/2){
                printf("%d",min(a.front(),b.front()));
                return 0;
            }
            if(a.front() < b.front()) a.pop();
            else b.pop();
            count++;
        }
        b.push(INF);
        for(; count < (n+m-1)/2; count++){
            if(a.front() < b.front()) a.pop();
            else b.pop();
        }
        printf("%d",min(a.front(),b.front()));
        return 0;
    } 

    //内存超限
    #include<cstdio> const int maxn = 1000010; const int INF = 0x7fffffff; int s1[maxn],s2[maxn]; int main(){ long long n,m; scanf("%d",&n); for(int i = 0 ; i < n; i++){ scanf("%d",&s1[i]); } scanf("%d",&m); for(int i = 0 ; i < m ; i++){ scanf("%d",&s2[i]); } s1[n] = s2[m] = INF; int medianPos = (n+m-1)/2; int count = 0, i = 0, j = 0; while(count < medianPos){ if(s1[i] < s2[j]) i++; else j++; count++; } if(s1[i] < s2[j]) printf("%d ",s1[i]); else printf("%d ",s2[j]); return 0; }

    20191011

    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    const int INF = 10000000;
    
    int main()
    {
        int n;
    
        int sum = 0;
        scanf("%d",&n);
        
        sum += n;
        int *arr1 = (int *)malloc(n * sizeof(int));
        for( i = 0; i < n; i++)
        {
            scanf("%d",&arr1[i]);
        }
        arr1[i] = INF;
        
        scanf("%d",&n);
        sum += n;
        int *arr2 = (int *)malloc(n * sizeof(int));
        for( i = 0 ; i < n; i++)
        {
            scanf("%d",&arr2[i]);
        }
        arr2[i] = INF;
        
        sum = (sum-1)/2;
        int i = 0;
        int j = 0;
        int mid;
        while(sum--)
        {
            if(arr1[i] >= arr2[j])
            {
                //mid = arr2[j];
                j++;
            } 
            else
            {
                //mid = arr1[i];
                i++;
            }
        }
        printf("%d",min(arr1[i],arr2[j]));
        return 0;
    }
    /*
    1.两个数组长度的中间位置 是 (n+m-1)/2  
    2.如果要按照比较数组值大小来逼近中间数,必须在两个数组的最后面加一个最大数
        主要是为了防止有一个数组的最小数大于另一个数组最大数的情况
    3.数组比较结束两个指向两个数字的位置,小的那个才是中间数 
    */
    #include<cstdio>
    #include<cstdlib>
    
    const int INF = 1000000000;
    
    int* GetNum(int *sum);
    
    int main()
    {
        int sum = 0;
        
        int *arr1 = GetNum(&sum);
        
        int *arr2 = GetNum(&sum);
        
        sum = (sum - 1)/2;
        int i = 0;
        int j = 0;
        while(sum--)
        {
            if(arr1[i] >= arr2[j])
            {
                j++;
            } 
            else
            {
                i++;
            }
        }
        printf("%d",arr1[i] > arr2[j] ? arr2[j] : arr1[i] );
        
        return 0;
    }
    
    int* GetNum(int *sum)
    {
        int n;
        scanf("%d",&n);
        *sum += n;
        
        int *arr = (int *)malloc(n * sizeof(int));
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&arr[i]);
        }
        arr[n] = INF;
        return arr;
    }
  • 相关阅读:
    使用.NET Core在RESTful API中进行路由操作
    基础教程:ASP.NET Core 2.0 MVC筛选器
    Angular 5和ASP.NET Core入门
    net core 使用tagHelper将 enum枚举类型转换为下拉列表select
    教你如何实现微信小程序与.net core应用服务端的无状态身份验证
    解决mssql localdb 中文乱码问题
    datagrid 新增,并行内编辑,提交保存
    合法的json数组字符串,转换json
    jfinal的回滚
    oracle 修改 字段名称
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/9462088.html
Copyright © 2020-2023  润新知