尼玛,数组偶数个数的时候取中位数是取中间两者中的前者,还tmd一直再算平均,卧槽

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector> using namespace std; int min(int a, int b) {
return a<b? a:b;
} int main() { int na, nb; scanf("%d", &na);
vector<int> a(na); for (int i=; i<na; i++) {
scanf("%d", &a[i]);
} scanf("%d", &nb);
vector<int> b(nb); for (int i=; i<nb; i++) {
scanf("%d", &b[i]);
} int ia = , ib = ; int idx = ;
int mid = (na + nb) / ; int mv = , mv2;
while (ia < na && ib < nb && idx < mid) {
if (a[ia] <= b[ib]) {
mv = a[ia];
ia++;
} else {
mv = b[ib];
ib++;
}
idx++;
} while (ia < na && idx < mid) {
mv = a[ia];
ia++;
idx++;
} while (ib < nb && idx < mid) {
mv = b[ia];
ib++;
idx++;
} if (ia < na && ib < nb) {
mv2 = min(a[ia], b[ib]);
} else if (ia < na) {
mv2 = a[ia];
} else if (ib < nb) {
mv2 = b[ib];
} if ((na + nb) & ) {
// odd
cout<<mv2<<endl;
} else {
// even
cout<<mv<<endl;
} system("pause");
return ;
}

log(na+nb)算法见:http://www.cnblogs.com/lailailai/p/3982103.html

PAT 1029. Median的更多相关文章

  1. PAT 1029 Median[求中位数][难]

    1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the midd ...

  2. PAT 1029 Median (25分) 有序数组合并与防坑指南

    题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...

  3. PAT甲 1029. Median (25) 2016-09-09 23:11 27人阅读 评论(0) 收藏

    1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...

  4. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  5. 浙大pat 1029题解

    1029. Median (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incre ...

  6. 1029 Median (25 分)

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  7. 【PAT】1029. Median (25)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  8. PAT 甲级 1029 Median

    https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...

  9. PAT Advanced 1029 Median (25) [two pointers]

    题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...

随机推荐

  1. elasticsearch 5.6.4自动创建索引与mapping映射关系 +Java语言

    由于业务上的需求 ,最近在研究elasticsearch的相关知识 ,在网上查略了大部分资料 ,基本上对elasticsearch的数据增删改都没有太大问题 ,这里就不做总结了  .但是,在网上始终没 ...

  2. mxonline实战10,课程列表页,课程详情页1

    对应github地址:第10天   一. 课程列表页   1. 拷贝course-list.html到templates目录中 2. 编写url和view 在courses/views.py中新加

  3. python------对于面向对象的理解

    python中一切皆为对象 其实面向对象没什么高大上的东西,只不过把我们平时对于事物的描述和动作系统的总结成了一个定义事物的方法而已. 我们平时向别人介绍一个他(她)从未见过的东西,会从外形和外貌特征 ...

  4. 对于一些seo查询网站的整理

    个人在查找网站seo问题时所使用的一些网站整理以及一些注意事项(同时感谢在处理问题过程中给予帮助的小伙伴们): 1.seo综合查询    (爱站网/站长工具)这俩工具的计算规则不相同所以计算结果出来也 ...

  5. LeetCode一句话题解

    深度优先搜索 人生经验 1. 需要输出所有解.并由于元素集有重复元素,要求返回的结果需要去重的情况,可考虑使用值对应数量的map,然后分别考虑依次取不同数量该值的可能. LeetCode39 题目:给 ...

  6. OAuth2.0认证和授权以及单点登录

    https://www.cnblogs.com/shizhiyi/p/7754721.html OAuth2.0认证和授权机制讲解 2017-10-30 15:33 by shizhiyi, 2273 ...

  7. vue.js - axios Get、Post方法传参给 .net core webapi。

    一:axios中是 Get请求: 1:在vue项目中通过params属性携带数据: let _self = this; axios({ method:'get', url:'http://localh ...

  8. Mac 10.12安装Command+Q误按提示工具

    说明:很多时候不小心会按强制关闭而无任何提示,这款工具能延迟关闭,并有相应的提示. 下载: (链接: https://pan.baidu.com/s/1bpyJMPL 密码: bqn1)

  9. PHP 删除 数组 指定成员

    1. unset删除某一个 特定成员 $arr[] = ; $arr[] = ; $arr[] = ; ]); var_dump($arr); array() { []=> ) []=> ...

  10. Android四种跨进程通信

    由于android系统中应用程序之间不能共享内存.因此,在不同应用程序之间交互数据(跨进程通讯)就稍微麻烦一些.在android SDK中提供了4种用于跨进程通讯的方式.这4种方式正好对应于andro ...