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×10^​5) 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

题意:将两个递增序列S1和序列S2,长度为n和m,将其合并成一个递增序列,然后输出其中位数。

思路:
1.开三个数组,直接将序列合并放入第三个数组中,然后输出中位数(m+n-1)/2(向下取整), 其中m+n-1是因为序列下标从0开始。例如长度为8应为3,长度为9应为4
2.开一个较大的数组,将两个序列读入后从小到大排序,然后输出中位数。
3.twopoints,令计数器count=0,表示当前已经获取到的新序列的位数。接下来就是类似序列合并问题。令i=j=0,不断比较S1[i]和S2[j]的大小
如果S[i]<S[j],说明新序列的当前位应该是S1[i],因此i++
否则,新序列的当前位应该是S2[j],j++
每确定一个当前最小的数,就让count++,直到count增加到(m+n-1)/2

code1

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 400010; //N最大值是2*10^5

int main()
{
int n, m, s1[maxn], s2[maxn], s3[maxn];
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]);
int i = 0, j = 0, k = 0;
while(i < n && j < m)
{
if(s1[i] <= s2[j])
s3[k++] = s1[i++];
// s3[k++] == s1[i++]; 神仙操作
else
s3[k++] = s2[j++];
// s3[k++] == s2[j++]; 神仙操作+1
}
while(i < n) s3[k++] = s1[i++];
while(j < m) s3[k++] = s2[j++];
printf("%d", s3[(m + n - 1) / 2]);
return 0;
}

code2

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 400010;
int s[maxn];
int main()
{
    int n, m;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d", &s[i]);
    scanf("%d", &m);
    for(int j = n; j < m + n; j++)
        scanf("%d", &s[j]);
    sort(s, s + m + n);
    printf("%d", s[(m+n-1)/2]);
    return 0;
}

code3

//在序列最后都加一个很大的数INF,这样在two points的扫描过程中,
//就可以在其中一个序列已经扫描完但count还没有到达中位数的情况下解决访问越界的问题
//1 2 0x7fffffff    i到达2后就不会再增加了,也就不会越界
//3 4 5 6 7 ......... 0x7fffffff
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1000010;
const int INF = 0x7fffffff; //int上限

int main()
{
    int n, m;
    int s1[maxn], s2[maxn];
    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 i = 0, j = 0, count = 0;
    int medianPos = (m + n - 1) / 2;    //中间位置
    while(count < medianPos)
    {
        if(s1[i] < s2[j])
            i++;
        else
            j++;
        count++;
    }
    if(s1[i] < s2[j])       //执行完上面的循环后count==medianPos,但是while循环中没有判断s1[i]和s2[j]的大小
        printf("%d", s1[i]);
    else
        printf("%d", s2[j]);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值