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} 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

解题思路

排序

AC代码

#include <cstdio>
#include <algorithm>
using namespace std;
vector<long long> v;
int main()
{
    long long n, t;
    scanf("%lld", &n);
    while (n--){
        scanf("%lld", &t);
        v.push_back(t);
    }
    scanf("%lld", &n);
    while (n--){
        scanf("%lld", &t);
        v.push_back(t);
    }
    nth_element(v.begin(), v.begin() + (v.size() - 1)/2, v.end());
    printf("%lld\n", v[(v.size() - 1)/2]);
    return 0;

}
时间: 2024-10-07 01:27:17

1029. Median (25)【排序】——PAT (Advanced Level) Practise的相关文章

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise

题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding i

1089. Insert or Merge (25)【排序】——PAT (Advanced Level) Practise

题目信息 1089. Insert or Merge (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, find

1098. Insertion or Heap Sort (25)【排序】——PAT (Advanced Level) Practise

题目信息 1098. Insertion or Heap Sort (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one el

1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise

题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For exa

1085. Perfect Sequence (25)【二分查找】——PAT (Advanced Level) Practise

题目信息 1085. Perfect Sequence (25) 时间限制300 ms 内存限制65536 kB 代码长度限制16000 B Given a sequence of positive integers and another positive integer p. The sequence is said to be a "perfect sequence" if M <= m * p where M and m are the maximum and minim

1083. List Grades (25)【水题】——PAT (Advanced Level) Practise

题目信息 1083. List Grades (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of w

1094. The Largest Generation (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1094. The Largest Generation (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the

1101. Quick Sort (25)【快排】——PAT (Advanced Level) Practise

题目信息 1101. Quick Sort (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are