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
  • 相关阅读:
    在centOS上安装oracle出现java.lang.NoClassDefFoundError问题及解决方法
    centos6.5下安装oracle11g
    配置单点登录
    CentOS 环境变量编辑、保存、立即生效的方法
    python如何调用C语言程序
    python生成exe可执行程序
    python的encode()和decode()函数
    python 获取时间
    python修改字符串的值
    python enumerate()函数
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8513944.html
Copyright © 2011-2022 走看看