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 }
  • 相关阅读:
    POJ 2418 Hardwood Species
    用Excel打开csv格式文件并生成相应图形
    虚拟内存(Virtual Memory)
    POJ 3984 迷宫问题 (Dijkstra)
    算法导论16.22 01背包问题
    POJ 1019 Number Sequence
    POJ 1458 Common Subsequence (最长公共子序列)
    Java处理文件BOM头的方式推荐
    web开发中的中文编码问题
    struts2学习笔记之spring整合
  • 原文地址:https://www.cnblogs.com/h-hkai/p/13141717.html
Copyright © 2011-2022 走看看