PAT 1029. Median (25)

1029. Median (25)

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

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 (<=1000000) 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

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

输入需要采用scanf输入
 1 #include <iostream>
 2 #include <cstdio>
 3
 4 using namespace std;
 5
 6 long seq1[1000000], seq2[1000000];
 7 long finalseq[2000000];
 8
 9 int main()
10 {
11     int n1, n2;
12     cin >> n1;
13     for (int i = 0; i < n1; i++)
14         scanf("%ld", &seq1[i]);
15     cin >> n2;
16     for (int i = 0; i < n2; i++)
17         scanf("%ld", &seq2[i]);
18
19     int i = 0, j = 0, k = 0;
20     while (i < n1 || j < n2)
21     {
22         if (i == n1)    //only seqence1
23             finalseq[k++] = seq2[j++];
24         else if (j == n2)    //only seqence2
25             finalseq[k++] = seq1[i++];
26         else if (seq1[i] < seq2[j])
27             finalseq[k++] = seq1[i++];
28         else if (seq1[i]>seq2[j])
29             finalseq[k++] = seq2[j++];
30         else
31         {
32             finalseq[k++] = seq1[i++];
33             j++;
34         }
35     }
36     cout << finalseq[(k - 1) / 2];
37 }
时间: 2024-10-02 13:48:50

PAT 1029. Median (25)的相关文章

1029. Median (25)【排序】——PAT (Advanced Level) Practise

题目信息 1029. Median (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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}

PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

1029 Median (25 分) 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 sequence

1029 Median (25 分)

1029 Median (25 分) 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 sequence

PAT Advanced 1029 Median (25分)

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

PAT:1029. Median (25) AC

#include<stdio.h> #include<algorithm> using namespace std; int arr1[1000066]; int arr2[1000066]; int main() { int n1,n2; scanf("%d",&n1); for(int i=0 ; i<n1 ; ++i) { scanf("%d",&arr1[i]); } scanf("%d",&

PAT (Advanced Level) 1029. Median (25)

scanf读入居然会超时...用了一下输入挂才AC... #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<queue> #include<iostream> #include<algorithm> using namespace std; const int maxn=1000000+10; int a[maxn],b

1029. Median (25)

时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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,

1029 Median (25分)

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

PAT 1029. Median

尼玛,数组偶数个数的时候取中位数是取中间两者中的前者,还tmd一直再算平均,卧槽 #include <iostream> #include <cstdio> #include <cstdlib> #include <vector> using namespace std; int min(int a, int b) { return a<b? a:b; } int main() { int na, nb; scanf("%d", &