1029 Median (25 分)
题目入口
思想
这题在考归并排序
然鹅STL的sort就完事了
还写了个set应该是超时了
有时间再改set吧
到这里讲解结束
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
long long a[400010];
int main(){
int n, m;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%lld", &a[n+i]);
sort(a, a+n+m+1);
int sum = n+m;
if (sum%2) printf("%lld\n", a[(sum+1)/2]);
else printf("%lld\n", a[sum/2]);
return 0;
}
TLE代码
#include <iostream>
#include <cstdio>
#include <set>
using namespace std;
set<long long> s;
int main(){
int n, m, num;
scanf("%d", &n);
for (int i = 0; i < n; i++){
scanf("%d", &num);
s.insert(num);
}
scanf("%d", &m);
for (int i = 0; i < n; i++){
scanf("%d", &num);
s.insert(num);
}
int sum = n+m;
set<long long>::iterator it = s.begin();
if (sum%2){
for (int i = 1; i < (sum+1)/2; i++)
it++;
printf("%lld\n", *it);
}
else {
for (int i = 1; i < sum/2; i++)
it++;
printf("%lld\n", *it);
}
return 0;
}