#include <stdio.h>
#include <stdlib.h>
#include <limits.h> #define N 1000000 int a1[N+1], a2[N+1]; int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
} int main(void)
{
int n1, n2, i1, i2, k, id, res; while (scanf("%d", &n1) != EOF)
{
for(i1=0; i1<n1; i1++)
scanf("%d", &a1[i1]);
scanf("%d", &n2);
for(i2=0; i2<n2; i2++)
scanf("%d", &a2[i2]); qsort(a1, n1, sizeof(a1[0]), cmp);
qsort(a2, n2, sizeof(a2[0]), cmp);
a1[n1] = INT_MAX;
a2[n2] = INT_MAX; i1 = i2 = k = 0;
id = (n1+n2-1)/2;
while (k < id)
{
k ++;
if (a1[i1] < a2[i2])
i1 ++;
else
i2 ++;
}
//printf("i1=%d, i2=%d, k=%d, id=%d\n", i1, i2, k, id);
if (a1[i1] < a2[i2])
res = a1[i1];
else
res = a2[i2];
printf("%d\n", res);
} return 0;
}
/**************************************************************
Problem: 1004
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:8724 kb
****************************************************************/

时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:16079

解决:4443

题目描述:

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年浙江大学计算机及软件工程研究生机试真题

思路:

本题的意思是求两个数组合并后数组的中间数。

实际上不需要全部合并再求,只需要从小到大求出合并后数组,求到中间数时终止即可输出结果。

其实也就是在执行归并排序,该排序有一个小技巧是将两个数组的队尾添加上限数,方便程序书写。

代码:

九度OJ 1004:Median的更多相关文章

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

    题目1004:Median 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:14162 解决:3887 题目描述: Given an increasing sequence S of N i ...

  2. 九度oj 1004

    题目1004:Median 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17536 解决:4860 题目描述:                        Given an incre ...

  3. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  4. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  5. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  6. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  7. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  8. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

  9. 九度OJ 1371 最小的K个数 -- 堆排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...

随机推荐

  1. Codeforces 161D Distance in Tree(树型DP)

    题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...

  2. bzoj1455&&luogu2713罗马游戏

    罗马游戏 题目描述 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最近举行了一次平面几何测试,每个人都得到了一个分数. 皇帝很喜欢平面几何,他对那些得分很低的人嗤之以鼻. ...

  3. 手机端用来上传用户头像的代码canvas

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 聊聊、Zookeeper 数据结构和操作命令

    Zookeeper 的视图结构跟标准的 Unix 文件系统很像,都有一个根节点 / .在根节点下面就是一个个的子节点,我们称为 ZNode.ZNode 是 Zookeeper 中最小数据单位,在 ZN ...

  5. Storyboards Tutorial 02

    内容中包含 base64string 图片造成字符过多,拒绝显示

  6. andriod GridLayout

    来自:http://blog.csdn.net/jianghuiquan/article/details/8299973 GridLayout网格布局 android4.0以上版本出现的GridLay ...

  7. 14.【nuxt起步】-Pm2 和nuxt服务运行

    1.安装pm2 npm install pm2 -gd 2.启动 Pm2 start ./bin/www 3. pm2 save 4.Pm2 startup 5.Pm2 save修改 package. ...

  8. 通过UltraISO来提取U盘启动盘的ISO镜像文件

    我们先来说下UltraISO这个工具,中文名也叫软碟通,他是一个无需量产你的U盘就可以把U盘做成启动盘的工具,当然了,这么强大的工具肯定不是免费版的,对,他是共享的:但是你可以下载特别版嘛..网上到处 ...

  9. 【C语言 C++】简单keywordRegister,Const,Static,Volatile,typedef,Define的理解

    Register 用register声明的变量称着寄存器变量,在可能的情况下会直接存放在机器的寄存器 中.但对32位编译器不起作用.当global optimizations(全局优化)开的时候,它会 ...

  10. GCC + GDB 调试方法

    首先编译程序  多加一个 -g c++ test.cpp -o a -Wall -g 执行时使用 gdb a 此时输入 l 显示所有的代码 l 输入b 加入断点到某一行(break) b 108 运行 ...