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×105) 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
解题思路
两个数组合并,找到中位数, 如果是偶数的话,取左边的数值。
这道题我是没有get到要考差的啥25分??? 直接一个sort就过了? , 之后在网上查了一下说超内存(long int ),实际上没有人家给的数就每超过int的范围吧
我写多种解法,多练练还是好的 ~ 个人认为用堆的思想解决比较好,实际上数组并不是有序的,只是输出了堆顶元素。
解题代码
解法一:
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000010], n, m;
int main(){
scanf("%d", &n);
for (int i = 0; i < n; i ++) scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = n; i < m + n; i ++) scanf("%d", &a[i]);
sort(a, a + n + m);
//int t = (m + n + 1) / 2 - 1;
int t = (m + n - 1) / 2;
printf("%d", a[t]);
return 0;
}
解法二: 归并排序
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000010], t[1000010], n, m;
void merge_sort(int l, int r){
if (l >= r) return;
int mid = (l + r) / 2, i = l, j = mid + 1, k = l;
merge_sort(l, mid), merge_sort(mid + 1, r);
while(i <= mid || j <= r){
if (j > r || (i <= mid && a[i] <= a[j])) t[k++] = a[i++];
else t[k++] = a[j++];
}
for (int i = l; i <= r; i++) a[i] = t[i];
}
int main(){
scanf("%d", &n);
for (int i = 0; i < n; i ++) scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = n; i < m + n; i ++) scanf("%d", &a[i]);
merge_sort(0, n + m);
// t = (m + n + 3) / 2 - 1
int t = (m + n + 1) / 2;
printf("%d", a[t]);
return 0;
}
解法三: 堆排序
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000010], n, m, len;
void down(int u){
int t = u;
if( u * 2 <= len && a [2 * u] < a[t]) t = 2 * u;
if (u * 2 + 1 <= len && a[2 * u + 1] < a[t]) t = 2 * u + 1;
if (t != u){
swap(a[t], a[u]);
down(t);
}
}
int main(){
scanf("%d", &n);
for (int i = 1; i <= n; i ++) scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = n + 1; i <= m + n; i ++) scanf("%d", &a[i]);
len = n + m;
for (int i = len / 2; i; i--) down(i);
int t = (m + n + 1) / 2 ;
while (t --){
if (t == 0) printf("%d", a[1]);
a[1] = a[len --];
down(1);
}
return 0;
}
小根堆 优先队列
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int n, t;
priority_queue<int, vector<int>, greater<int>> q;
int main(){
scanf("%d", &n);
while (n--){
scanf("%d", &t);
q.push(t);
}
scanf("%d", &n);
while (n--){
scanf("%d", &t);
q.push(t);
}
t = (q.size() - 1) / 2;
while (t--) q.pop();
printf("%d", q.top());
return 0;
}
今天刷题了没
发布了190 篇原创文章 · 获赞 39 · 访问量 1万+
私信
关注