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 }
相关文章
- C. Maximum Median 二分
- leetcode 之Median of Two Sorted Arrays(五)
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
- [Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript
- LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard
- [Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript
- [LeetCode] Find Median from Data Stream
- 【35】报错Maximum disparaity value ‘1520‘ exceeds the maximum supported 1024‘ by median filter. Disabli
- 【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) B】Reach Median
- 【22.70%】【codeforces 591C】 Median Smoothing
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
- [LeetCode] 295. Find Median from Data Stream ☆☆☆☆☆(数据流中获取中位数)
- lintcode 1: Data Stream Median
- LeetCode Median of Two Sorted Arrays
- 1029. Median (25)
- Median of Two Sorted Arrays--LeetCode
- poj 1975 Median Weight Bead(传递闭包 Floyd)
- LC:4. Median of Two Sorted Arrays
- PAT 1029 Median[求中位数][难]
- HDU5857 Median 模拟
- python 利用quick sort思路实现median函数
- spark rdd median 中位数求解
- 刷题记录:牛客NC50940Running Median
- Pandas-常用统计分析方法 describe、quantile、sum、mean、median、count、max、min、idxmax、idxmin、mad、var、std、cumsum