题目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. win10登录不上sql2005

    因需求安装了一个SQL Server 2005. 登录的时候,用户Sa模式可以登录,Windows身份验证方式却无法登录. 经测试.解决方法如下: SQL Server Management Stud ...

  2. 超级简单的例子说明JAVA PACKET CLASS 和变量之间的关系

    一.包PACKET 就是一个文件夹,包下的CLASS互相访问如一个文件. 二.class内部相当于一个DELPHI的calss,静态函数(static )只能访问静态函数. package Mainp ...

  3. 27款经典的CSS框架

    利用 CSS 框架,可以简化你的工作,提高工作效率.CSS 框架是一系列 CSS 文件的集合体,包含了基本的元素重置,页面排版.网格布局.表单样式.通用规则等代码块.下面给你推荐了27款优秀的CSS框 ...

  4. python-自定义异步非阻塞爬虫框架

    api import socket import select class MySock: def __init__(self, sock, data): self.sock = sock self. ...

  5. Jquery 【on事件】

    一.on事件的定义和用法 on() 方法在被选元素及子元素上添加一个或多个事件处理程序. 自 jQuery 版本 1.7 起,on() 方法是 bind().live() 和 delegate() 方 ...

  6. Maximum splitting(规律,数论)

    You are given several queries. In the i-th query you are given a single positive integer ni. You are ...

  7. webpack中设置jquery为全局对象

    通过npm安装jquery npm install jquery -D 然后配置webpack-config.js plugins: [ new webpack.ProvidePlugin({ $: ...

  8. 玩转MongoDB

    一.主从复制 1)首先主从服务器在启动的时候,分别要设置master.slave选项,对于slave可以启动中设置源,也可以在启动后设置源. 如:主:mongod --dbpath=/data/mon ...

  9. 趣味测试类微信小程序

    先说说项目需求吧, l  点击[再测一次],重新开始测试流程,主持人回复第一个题目,流程同上:答完全部题目后,底部不显示[立即开始分析]按钮,而是直接展示结果,且上一次测试内容不清空:如退出再进来,则 ...

  10. linux ssh 上传 文件

    在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件scp username@servername:/path/filename /var/www/local_dir(本地 ...