题目1004:Median

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:14162

解决:3887

题目描述:

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 non-decreasing 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.

输入:

Each input file may contain more than one test case.
    Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) 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.

输出:

For each test case you should output the median of the two given sequences in a line.

样例输入:
4 11 12 13 14
5 9 10 15 16 17
样例输出:
13
来源:
2011年浙江大学计算机及软件工程研究生机试真题

分析:

模拟归并:

 #include <cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int a[],b[];
int main()
{
int n,m;
while(cin>>n){
int i;
for(i=;i<n;i++){
scanf("%d",&a[i]);
}
cin>>m;
for(i=;i<m;i++){
scanf("%d",&b[i]);
}
int j=;
i=;
int num=(m+n-)/;
while(i<n&&j<m){
if(a[i]>b[j]){
j++;
num--;
if(num<){
cout<<b[j-]<<endl;
break;
}
}
else{
i++;
num--;
if(num<){
cout<<a[i-]<<endl;
break;
}
}
}
if(num>=){
while(i<n){
num--;
if(num<){
cout<<a[i]<<endl;
break;
}
i++;
}
while(j<m){
num--;
if(num<){
cout<<b[j]<<endl;
break;
}
j++;
}
}
}
return ;
}

九度oj 1004 Median 2011年浙江大学计算机及软件工程研究生机试真题的更多相关文章

  1. 九度oj 1002 Grading 2011年浙江大学计算机及软件工程研究生机试真题

    #include<iostream> #include<queue> #include<cstdio> #include<cstring> #inclu ...

  2. 九度oj 1032 ZOJ 2009年浙江大学计算机及软件工程研究生机试真题

    题目1032:ZOJ 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4102 解决:2277 题目描述: 读入一个字符串,字符串中包含ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出,当 ...

  3. 九度oj 1468 Sharing 2012年浙江大学计算机及软件工程研究生机试真题

    题目1468:Sharing 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2687 解决:550 题目描述: To store English words, one method is ...

  4. 九度oj 1001 A+B for Matrices 2011年浙江大学计算机及软件工程研究生机试真题

    题目1001:A+B for Matrices 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15235 解决:6172 题目描述: This time, you are supposed ...

  5. 九度oj 1034 寻找大富翁 2009年浙江大学计算机及软件工程研究生机试真题

    题目1034:寻找大富翁 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5323 解决:2123 题目描述:     浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. 输入:     ...

  6. 九度oj 1031 xxx定律 2009年浙江大学计算机及软件工程研究生机试真题

    题目1031:xxx定律 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5153 解决:3298 题目描述:     对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n ...

  7. 九度oj 1006 ZOJ问题 2010年浙江大学计算机及软件工程研究生机试真题

    题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:16244 解决:2742 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC.是 ...

  8. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  9. 九度oj 1464 Hello World for U 2012年浙江大学计算机及软件工程研究生机试真题

    题目1464:Hello World for U 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3872 解决:1082 题目描述: Given any string of N (> ...

随机推荐

  1. Reporting Service服务SharePoint集成模式安装配置(7、配置SharePoint2010产品)

    在第3步安装完成SharePoint2010产品后,没有选择[立即安装产品配置向导],这一小节将单独配置SharePoint2010产品数据库(管理中心). 1)启动SharePoint 2010 产 ...

  2. js ~或者~~

    问题:~是什么意思? 答:js中是对数字取反 var a = null; var b = '23' console.log(~~null) console.log(~~b)

  3. 理解ASP.NET MVC引擎处理模型字符串的默认行为,涉及Html.Raw()和HttpUtility.HtmlDecode()

    MVC引擎默认会将Model属性值中包含的html字符串做encode,所以如属性中包含类似 <br> 这样的标记,MVC引擎会将它们转换成html实体码 %3Cbr%3E 所以1:在编辑 ...

  4. HTML5 开发APP 第一章

    当今天下,移动端基本上是安卓和苹果的天下,基本上没微软什么事,作为微软忠实的支持者,也要顺势而变. 但安卓和IOS  开发是两个完全不同的世界,有没有一种技术,开发完以后可以运行在任意终端呢,答案是有 ...

  5. 8 个用于生产环境的 SQL 查询优化调整

    在没有数据仓库或单独的分析数据库的组织中,报告的唯一来源和最新的数据可能是在现场生产数据库中. 在查询生产数据库时,优化是关键.一个低效的查询可能会对生产数据库产生大量的资源消耗,如果查询有错误会引发 ...

  6. 40. 组合总和 II leetcode JAVA

    题目: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使 ...

  7. HTML5中本地储存概念是什么,什么优点 ,与cookie有什么区别?

    html5中的Web Storage 包括了两种存储方式: sessionStorage  和  localStorage. seessionStorage 用于本地存储一个会话(session)中的 ...

  8. 【CF453D】 Little Pony and Elements of Harmony(FWT)

    题面 传送门 设\(a\)的递推公式为 \[a_i=\sum_ja_jb[count(i\oplus j)]\] 其中\(\oplus\)为异或,\(count(i)\)表示\(i\)的二进制中\(1 ...

  9. [ActionScript 3.0] 判断XML属性是否存在

    在as3中判断xml节点是否存在以及判断xml某节点是否存在某属性可用下面方法: if(xml.hasOwnProperty("frameRate")){ trace(" ...

  10. 【转】【C++专题】C++ sizeof 使用规则及陷阱分析

    提示:下文例子都经过Visual C++ 6.0验证,平台为win32 Windows. 一.什么是sizeof 首先看一下sizeof在msdn上的定义: The sizeof keyword gi ...