• PAT1029.Median (25)


    (一)题目

    题目链接:https://www.patest.cn/contests/pat-a-practise/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

    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 (<=1000000) 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

    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)题目意思已经很明确了,就是要寻找两个有序数组合并成一个有序数组后中间的那个数。如果合并后数组长度为偶数则输出中间两个数中左边的那个。
    因而寻找的数在合并后的下标应该是target = n % 2 ? n / 2 : n / 2 - 1;
    (2)我的做法是设置两个下标i,j分别对应s1数组和s2数组。i + j的值就很好的反映了合并后对应的下标。问题是如何标记当前位置是s1[i]还是s2[j]?
    期初我是设置了一个flag标记,flag为0就认为是s1[i]否则就认为是s2[j]。测试发现这只能反映上一轮的情况,而不能决定最终的情况。尤其是当i或j走到头的时候。。。
    (3)给一些测试用例吧
    Input1
    4 1 1 1 1
    5 2 2 2 2 2
    Output1
    2

    Input2
    5 1 2 3 4 5
    4 1 6 7 8
    Output2
    4

    Input3
    6 1 1 1 1 1 10
    5 2 2 2 2 2
    Output 3
    2

    Input4
    5 1 2 3 4 5
    5 1 2 3 4 5
    Output 4
    3
    (三)AC源码
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 #define maxn 1000005
     4 #define For(I,A,B) for(int I = (A); I < (B); I++)
     5 
     6 long long s1[maxn],s2[maxn];
     7 int n1,n2,n;
     8 int main()
     9 {
    10     freopen("1029.in","r",stdin);
    11     while(scanf("%d",&n1) != EOF)
    12     {
    13         For(i,0,n1)
    14             scanf("%lld",&s1[i]);
    15         scanf("%d",&n2);
    16         For(i,0,n2)
    17             scanf("%lld",&s2[i]);
    18         n = n1 + n2;
    19         int target = (n % 2) ? n / 2 : n / 2 - 1;
    20         int i = 0,j = 0;
    21         //bool flag = 0;
    22         //cout<<target<<" !
    ";
    23         while(i < n1 && j < n2 && i + j < target)
    24         {
    25             if(s1[i] < s2[j])
    26             {
    27                 i++;
    28                 //flag = 0;
    29             }
    30             else
    31             {
    32                 j++;
    33                 //flag = 1;
    34             }
    35             //cout<<i<<" "<<j<<" "<<endl;
    36         }
    37         if(i + j < target)
    38         {
    39             while(i < n1 && i + j < target)
    40             {
    41                 i++;
    42                 //flag = 0;
    43             }
    44             while(j < n2 && i + j < target)
    45             {
    46                 j++;
    47                 //flag = 1;
    48             }
    49         }
    50         //if(j == n2) j--;
    51         //if(i == n1) i--;
    52         if(j == n2 || (i != n1 && s1[i] < s2[j]))
    53         {
    54             printf("%lld
    ",s1[i]);
    55         }
    56         else if(i == n1 || s1[i] >= s2[j])
    57         {
    58             printf("%lld
    ",s2[j]);
    59         }
    60     }
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    黄聪:PHP获取MAC地址(转)
    黄聪:HTC同步出现太多联系人删除项解决方法
    黄聪:WordPress Cron 定时任务(转)
    黄聪:wordpress后台发布文章,自定义栏目和字段
    黄聪:如何在wordpress任何一个页面调用“插入图片”按钮,实现上传图片功能
    黄聪:获取搜索引擎来源访客的搜索关键词
    黄聪:用php判断当前用户访问网站是否为手机登录
    黄聪:php计算获取页面执行时间
    黄聪:抓取网页链接的php类:snoop
    黄聪:解决wordpress定时发布文章失败”丢失计划任务”的插件
  • 原文地址:https://www.cnblogs.com/xiaozhuyang/p/6112884.html
Copyright © 2020-2023  润新知