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
-
思路1:暴力法
排序,输出ans[(len - 1) / 2]
-
code :
#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
int main()
{
int n;
for(int i = 0; i < 2; ++i)
{
scanf("%d", &n);
for(int j = 0; j < n; ++j)
{
int tmp;
scanf("%d", &tmp);
ans.push_back(tmp);
}
}
sort(ans.begin(), ans.end());
printf("%d", ans[(ans.size()-1)/2]);
return 0;
}
-
思路2:双指针法:
类似于Merge,i,j指向两个数组的头,每次取小,从前往后数到第(len - 1) / 2个数,输出即可 -
code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 400010, INF = 0x3fffffff;
int s1[maxn], s2[maxn];
int main(){
int n, m, tmp;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &s1[i]);
}
scanf("%d", &m);
for(int i = 0; i < m; ++i){
scanf("%d", &s2[i]);
}
int i = 0, j = 0, cnt = (n + m - 1) / 2;
s1[n] = INF, s2[m] = INF; //Wrong 1: 若不限制边界 样例6出错 -2
while(cnt--){
if(s1[i] < s2[j]) i++;
else j++;
}
printf("%d", min(s1[i], s2[j]));
return 0;
}
- T3 code: 显然还是哨兵好!
#include <bits/stdc++.h>
using namespace std;
vector<int> ans[2];
int main()
{
int n, sum = 0;
for(int i = 0; i < 2; ++i)
{
scanf("%d", &n);
sum += n;
ans[i].resize(n);
for(int j = 0; j < n; ++j)
{
scanf("%d", &ans[i][j]);
}
}
int idex1 = 0, idex2 = 0, cnt = (sum - 1) / 2;
while(idex1 < ans[0].size() && idex2 < ans[1].size() && cnt > 0)
{
if(ans[0][idex1] < ans[1][idex2]) idex1++;
else idex2++;
cnt--;
}
while(cnt--)
{
idex1++; idex2++;
}
if(idex1 >= ans[0].size()) printf("%d", ans[1][idex2]);
else if(idex2 >= ans[1].size()) printf("%d", ans[0][idex1]);
else
{
int median = min(ans[0][idex1], ans[1][idex2]);
printf("%d", median);
}
return 0;
}
- T3 code:
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
vector<int> ans[2];
int main()
{
int n, sum = 0;
for(int i = 0; i < 2; ++i)
{
scanf("%d", &n);
sum += n;
ans[i].resize(n+1);
for(int j = 0; j < n; ++j)
{
scanf("%d", &ans[i][j]);
}
ans[i][n] = INF; //哨兵:到底了直接卡住继续执行
}
int idex1 = 0, idex2 = 0, cnt = (sum - 1) / 2;
while(cnt--)
{
if(ans[0][idex1] < ans[1][idex2]) idex1++;
else idex2++;
}
int median = min(ans[0][idex1], ans[1][idex2]);
printf("%d", median);
return 0;
}
- T4 code:
#include <bits/stdc++.h>
using namespace std;
vector<int> num[2];
int main()
{
int idex = 0;
for(int i = 0; i < 2; ++i)
{
int n;
scanf("%d", &n);
num[i].resize(n);
for(int j = 0; j < n; ++j)
{
scanf("%d", &num[i][j]);
}
}
int i = 0, j = 0, cnt = (num[0].size() + num[1].size() + 1) / 2;
while(cnt && i < num[0].size() && j < num[1].size())
{
if(num[0][i] < num[1][j]) i++;
else j++;
cnt--;
}
if(cnt == 0)
{
printf("%d", max(num[0][i-1], num[1][j-1]));
return 0;
}
while(cnt > 0)
{
i++; j++;
cnt--;
}
printf("%d", i >= num[0].size() ? num[1][j-1] : num[0][i-1]);
return 0;
}