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
  • 相关阅读:
    怎么把分页按钮(首页,尾页等)放在表格右下角处?(已解决)
    zabbix单位符号
    容器、可迭代对象、迭代器、生成器之间的关系.
    Zabbix housekeeper processes more than 75% busy
    zabbix 告警信息模板
    zabbix 历史数据和趋势数据
    socket沾包问题
    面向对象--进阶
    面向对象
    列表 元组 字典
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8513944.html
Copyright © 2011-2022 走看看