PAT 1029. Median
尼玛,数组偶数个数的时候取中位数是取中间两者中的前者,还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的更多相关文章
- PAT 1029 Median[求中位数][难]
1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the midd ...
- PAT 1029 Median (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- 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 ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 浙大pat 1029题解
1029. Median (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incre ...
- 1029 Median (25 分)
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 【PAT】1029. Median (25)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- PAT 甲级 1029 Median
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...
- 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 ...
随机推荐
- jQuery入门讲解
jQuery设计思想: (1)选择页面元素 css选择器: $(document) 选择整个文档对象, $("#myId") 选择id为myId的页面元素, $("div ...
- 敏感词过滤的算法原理之DFA算法
参考文档 http://blog.csdn.net/chenssy/article/details/26961957 敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有 ...
- 零基础学习Python数据分析
网上虽然有很多Python学习的教程,但是大多是围绕Python网页开发等展开.数据分析所需要的Python技能和网页开发等差别非常大,本人就是浪费了很多时间来看这些博客.书籍.所以就有了本文,希望能 ...
- Azure CLI2.0 捕获Linux ARM非托管磁盘虚拟机并创建ARM托管磁盘虚拟机
1.系统内部取消预配VM,创建了测试文件目录及文件:hlm20180904/ hlm20180904.txt 2.使用CLI2.0创建VM映像 a.登陆CLI2.0 备注:在 Azure 中国区使用 ...
- git 命令摘录
回滚 n 个 commit (增加了revert commit) git revert -n commit_id 回滚到指定的commit_id(不增加commit,回滚的commit_id被删除) ...
- 架构师养成记--26.vi/vim相关操作
vi/vim命令模式插入模式 aio编辑模式 : aio就是vi/vim的插入模式命令 作用a 在光标后附加文本A 在本行末附加文本i 在光标钱插 ...
- 编程开发之--java多线程学习总结(2)同步代码块
1.第一种解决办法:同步代码块,关键字synchronized package com.lfy.ThreadsSynchronize; /** * 1.使用同步代码块 * 语法: synchroniz ...
- 基于CentOS7系统一键配置Aria2 实现服务器离线下载工具
我们有些网友购买的海外VPS主机并不是用来做网站的,而是用来作为下载资源工具使用的.确实用这样的工具搭建之后是比本地下载速度快,因为有些资源.软件等是海外资源,而且挂载在服务器上不占用本地的资源.在这 ...
- Java将\替换成/
public static void main(String[] args) { String str="upload\\media\\201904\\i4Qjz8E40xGQovUq-2C ...
- Android中自定义组合控件
Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...