尼玛,数组偶数个数的时候取中位数是取中间两者中的前者,还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. RHEL安装oracle客户端(版本为11.2)

    RHEL安装oracle客户端(版本为 11.2) 1.获取超级管理员 命令:su - 输入root密码 2.安装依赖包: 命令:yum install -y binutils compat-libs ...

  2. LLDB 和Chisel 使用例子

    打印变量 打印数字 (lldb) p/d 16 16 16 进制格式 (lldb) p/x 16 0x10 2 进制格式 (lldb) p/t 16 0b00000000000000000000000 ...

  3. 代码 | 自适应大邻域搜索系列之(3) - Destroy和Repair方法代码实现解析

    前言 上一篇文章中我们具体解剖了ALNS类的具体代码实现过程,不过也留下了很多大坑.接下来的文章基本都是"填坑"了,把各个模块一一展现解析给大家.不过碍于文章篇幅等原因呢,也不会每 ...

  4. Machine learning第6周编程作业

    1.linearRegCostFunction: function [J, grad] = linearRegCostFunction(X, y, theta, lambda) %LINEARREGC ...

  5. 洛谷P2051 [AHOI2009]中国象棋(dp)

    题面 luogu 题解 \(50pts:\)显然是\(3\)进制状压\(dp\) \(100pts:\) 一行一行地考虑 \(f[i][j][k]\)表示前\(i\)行,有\(j\)列放了一个,有\( ...

  6. 一种很有意思的数据结构:Bitmap

    昨晚遇到了一种很有意思的数据结构,Bitmap. Bitmap,准确来说是基于位的映射.其中每个元素均为布尔型(0 or 1),初始均为 false(0).位图可以动态地表示由一组无符号整数构成的集合 ...

  7. windows server 2012 valid key

    好吧,网页三剑客. 1, load disc iso 2,check ip settings, 3,net-inst-server-start 4,power Node, F2 4.1 F7 usbc ...

  8. 【Kafka】Broker之Server.properties的重要参数说明

    名称 描述 类型 默认值 有效值区间 重要程度 zookeeper.connect zk地址 string 高 advertised.host.name 过时的:只有当advertised.liste ...

  9. Git fetch & pull 区别

    1 简单概括 2 git fetch 的用法 3 git pull的用法 文章来源:https://blog.csdn.net/qq_36113598/article/details/78906882

  10. Python基础 - 总则

    学习Python的笔记,有基础语法,有注意点.仅此而已. 目录: ------------------------------------------- Python基础(1) - 初识Python ...