zoukankan      html  css  js  c++  java
  • 1029 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 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 (≤) 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

    题意:

      寻找两个按照升序排好序的序列,合并后的中位数。

    思路:

      直接用sort排序然后找出中位数就好了,需要注意的使如果使用cin进行输入的话,最后一组数据会被卡到。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main() {
     6     int n1, n2, t;
     7     scanf("%d", &n1);
     8     vector<int> v(400005);
     9     for (int i = 0; i < n1; ++i) scanf("%d", &v[i]);
    10     scanf("%d", &n2);
    11     for (int i = n1; i < n2 + n1; ++i) scanf("%d", &v[i]);
    12     int mid = (n1 + n2 + 1) / 2;
    13     sort(v.begin(), v.begin() + n1 + n2);
    14     cout << v[mid - 1] << endl;
    15     return 0;
    16 }
  • 相关阅读:
    nvidia tx1使用记录--基本环境搭建
    STL hashtable阅读记录
    Linux strace命令
    转一篇:Reactor模式
    C++ 模板特化以及Typelist的相关理解
    C++ 内联函数inline
    迭代器失效的几种情况总结
    C++ Class与Struct的区别
    C++中string.find()函数,string.find_first_of函数与string::npos
    C/C++ 中长度为0的数组
  • 原文地址:https://www.cnblogs.com/h-hkai/p/13141717.html
Copyright © 2011-2022 走看看