【一隻蒟蒻的刷題歷程】 【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;
}

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