zoukankan      html  css  js  c++  java
  • A1029. 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

    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 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 using namespace std;
     5 int num1[1000001], num2[1000001];
     6 int main(){
     7     int N1, N2;
     8     scanf("%d", &N1);
     9     for(int i = 0; i < N1; i++){
    10         scanf("%d", &num1[i]);
    11     }
    12     scanf("%d", &N2);
    13     for(int i = 0; i < N2; i++){
    14         scanf("%d", &num2[i]);
    15     }
    16     int index = -1, ans = 0, i = 0, j = 0, mid = (N1 + N2 - 1) / 2;
    17     while(i < N1 && j < N2 && index != mid){
    18         if(num1[i] < num2[j])
    19             ans = num1[i++];
    20         else ans = num2[j++];
    21         index++;
    22     }
    23     while(i < N1 && index != mid){
    24         ans = num1[i++];
    25         index++;
    26     }
    27     while(j < N2 && index != mid){
    28         ans = num2[j++];
    29         index++;
    30     }
    31     printf("%d", ans);
    32     cin >> N1;
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    #define、const、typedef的区别
    《软件调试的艺术》学习笔记——GDB使用技巧摘要
    作为员工与老板的竞争力区别
    部分经典IT书籍
    对找工作功不可没——评《深入理解计算机系统》
    前端UI框架小汇总
    Visual Studio 2017 序列号 Key 激活码 VS2017 注册码
    Wampserver 2.5 多站点配置方法
    ThinkPHP3.1快速入门(9)变量输出
    ThinkPHP3.1快速入门(8)视图
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8513944.html
Copyright © 2011-2022 走看看