【一只蒟蒻的刷题历程】 【PAT】 A1029 中位数

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 (≤2×10​5​​) 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


题目大意:

给定N个整数的递增序列S,中值是中间位置的数字。例如,S1={11,12,13,14}的中值是12,S2={9,10,15,16,17}的中值是15。将两个序列的中值定义为包含两个序列所有元素的非递减序列的中值。例如,S1和S2的中值是13。

给定两个递增的整数序列,要求您找到它们的中值。

输入规格:

每个输入文件包含一个测试用例。每个案例占据2行,每行给出一个序列的信息。对于每个序列,第一个正整数N(≤2×105)是该序列的大小。接下来是N个整数,用空格隔开。保证所有整数都在长整数范围内。

输出规格:

对于每个测试用例,您应该在一行中输出两个给定序列的中值。

样本输入:

4 11 12 13 14

5 9 10 15 16 17

样本输出:

13个


代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int a[1000000];  //数组有点大,放外面
int main() 
{
  int n,c=0;
  cin>>n;
  for(int i=0;i<n;i++)
     scanf("%d",&a[c++]); //用cin最后一个测试点过不了
  cin>>n;
   for(int i=0;i<n;i++)
     scanf("%d",&a[c++]);
  sort(a,a+c);  //排序
  if(c%2==1) cout<<a[c/2]; //个数为奇数 
  else cout<<a[c/2-1];    //个数为偶数
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章