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 }
  • 相关阅读:
    Python菜鸟之路:Django 序列化数据
    Python菜鸟之路:Django 数据验证之钩子和Form表单验证
    Python菜鸟之路:Django 路由补充1:FBV和CBV
    Python菜鸟之路:Django 文件上传的几种方式
    Python菜鸟之路:Django 分页
    Python菜鸟之路:Django 信号
    Python菜鸟之路:Django 缓存
    《将博客搬至CSDN》
    Java基础语法
    Java基础语法
  • 原文地址:https://www.cnblogs.com/h-hkai/p/13141717.html
Copyright © 2011-2022 走看看