zoukankan      html  css  js  c++  java
  • pat advanced level 1029

    1029 Median(25 分)

    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 (2×105​​) 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
    
    /**********************
    author: yomi
    date: 18.6.15
    ps:
    long long int 都内存超限
    改掉了之后 还剩最后一个样例内存超限。暂时弃疗。
    还有一个问题: 为啥cnt < midPos?因为我们要找的是midPos啊 cnt == midPos跳出循环,此时比较a[i],b[j]即得结果。
    我一直纠结 为什么下标为0还是下标为1, 都是cnt<midPos, 仔细想一想, 下标为0还是1只是决定了cnt的初值是0还是1,
    最终退出条件都是cnt<midPos,因为我们要找的是midPos啊 cnt == midPos跳出循环,此时比较a[i],b[j]即得结果。
    **********************/
    #include <cstdio>
    
    ///--- O(logn) ---
    const int maxn = 1000002;
    const int inf = 0x7fffffff;
    int a[maxn], b[maxn];
    
    int main()
    {
        int n, m, cnt = 1;
        scanf("%d", &n);
        for(int i=1; i<=n; i++){
            scanf("%d", &a[i]);
        }
        scanf("%d", &m);
        for(int i=1; i<=m; i++){
            scanf("%d", &b[i]);
        }
        a[n+1] = inf, b[m+1] = inf;///我以为这里只是防止一个数组扫描完,会造成数组越界的。然而这句做的可不止这些。
        int midPos = (m+n+1)/2; /// 12345         ///最后比较的时候,如果有一个数组扫描完了,那就是这个数组的最后一个元素的下一个元素
        int i=1, j=1;                              ///和另一个数组的当前元素比大小了,这就出现问题了,必须让最后一个元素的下一个为inf
        while(cnt < midPos /*&& i<=n && j<=m没必要*/){        ///最好直接memset为inf吧
            if(a[i] < b[j]){
                i++;
            }
            else{
                j++;
            }
            cnt++;
        }
    //    while(cnt<midPos && i<=n){   ///加上了inf 这里就没必要写了
    //        i++;
    //        cnt++;
    //    }
    //    while(cnt<midPos && j<=m){
    //        j++;
    //        cnt++;
    //    }
        //cout << i << ' ' << j <<endl;
        if(a[i] < b[j])
            printf("%d", a[i]);
        else
            printf("%d", b[j]);
        return 0;
    }
    
    /**
    4 11 12 13 14
    5 9 10 15 16 17
    13
    **/
    
    /**    ---   O(nlogn)  ----
    long long int a[2000010];
    
    int Partition(int left, int right)
    {
        int temp = a[left];
        while(left < right){
            while(left<right && a[right]>temp){
                right--;
            }
            a[left] = a[right];
            while(left<right && a[left]<=temp){
                left++;
            }
            a[right] = a[left];
        }
        a[left] = temp;
        return left;
    }
    void quicksort(int left, int right)
    {
        if(left < right){
            int pos = Partition(left, right);
            quicksort(left, pos-1);
            quicksort(pos+1, right);
    
        }
    
    }
    int main()
    {
        int n, m;
        scanf("%d", &n);
        for(int i=1; i<=n; i++){
            scanf("%lld", &a[i]);
        }
        scanf("%d", &m);
        for(int i=1; i<=m; i++){
            scanf("%lld", &a[i+n]);
        }
        //sort(a, a+m+n);
        quicksort(1, m+n);
    //    for(int i=1; i<=m+n; i++){
    //        cout << a[i] <<' ';
    //    }
        printf("%lld", a[(m+n+1)/2]);
    
        return 0;
    }
    **/
  • 相关阅读:
    telnet命令找不到问题
    hive向表中执行insert语句报错问题
    SharePoint 2010 日期控件(DateTimeControl)的用法
    SharePoint 2010 启用InfoPath支持
    Sharepoint 2010 根据用户权限隐藏Ribbon菜单
    Sharepoint 2010 使用feature部署文件
    SharePoint 2010 查询不以某个字符开头的数据[How to Create a SharePoint “Does Not Begin With” Filtered List View]计算栏的妙用
    SharePoint 2010 栏计算经验收集
    SharePoint 2010 更加列表栏的值显示不同的背景颜色
    SharePoint 2010 使用SP.UI.ModalDialog.showModalDialog(options)对话框框架传值
  • 原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/9236351.html
Copyright © 2011-2022 走看看