九度OJ 1004:Median
#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
思路:
本题的意思是求两个数组合并后数组的中间数。
实际上不需要全部合并再求,只需要从小到大求出合并后数组,求到中间数时终止即可输出结果。
其实也就是在执行归并排序,该排序有一个小技巧是将两个数组的队尾添加上限数,方便程序书写。
代码:
九度OJ 1004:Median的更多相关文章
- 九度oj 1004 Median 2011年浙江大学计算机及软件工程研究生机试真题
题目1004:Median 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:14162 解决:3887 题目描述: Given an increasing sequence S of N i ...
- 九度oj 1004
题目1004:Median 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17536 解决:4860 题目描述: Given an incre ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- 九度OJ,题目1089:数字反转
题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...
- 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...
- 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
随机推荐
- python接口自动化(四十)- logger 日志 - 下(超详解)
简介 按照上一篇的计划,这一篇给小伙伴们讲解一下:(1)多模块使用logging,(2)通过文件配置logging模块,(3)自己封装一个日志(logging)类.可能有的小伙伴在这里会有个疑问一个l ...
- 关于bug的沟通
关于BUG的沟通 一个人要去做一件事情,一般来说是按照自己的意愿去做的,如果不是自己想做而是被要求这么做的话,心里一定会留下点不愉快,特别是那种有自信有自己主见的人,比如说开发人员,当测试人员发现一个 ...
- 泽熙学到的 z
叶展,原泽熙投资总经理助理,现任齐鲁证券资产管理公司总裁助理,齐鲁星空.星汉等集合理财投资经理. 导读:三年前,我加入了泽熙投资,正式成为一名职业投资者.做职业投资者一直是我的理想.在股市中用眼光和头 ...
- 如何让你的网页加载时间降低到 1s 内
当初分析了定宽高值和定宽高比这两种常见的图片延迟加载场景,也介绍了他们的应对方案,还做了一点技术选型的工作. 经过一段时间的项目实践,在先前方案的基础上又做了很多深入的优化工作.最终将好奇心日报的网页 ...
- HTML小技巧将table边框改为细线
HTML制作新手在用TABLE表格时,会碰到如何改变边线粗线,因为默认的TABLE边线设置即使是1px 是很粗的.因此会使用其他一些方法来制作出细线边框,这里介绍一种利用CSS来实现细线的方法,很有效 ...
- php hex2bin 物联网设备发送十六进制数据
hex2bin("十六进制字符") 折腾了三天 后来大神一句话搞定 : hex2bin("十六进制字符") 网上有个方向是错误的 就是 "\xAA\x ...
- hdu1394 Minimum Inversion Number(最小逆序数)
Minimum Inversion Number Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/O ...
- Sql 复习3之存储管理
一.事务管理 单个工作单元称为事务,我们将若干条sql命令组合在一起,形成存储过程.触发器等,利用存储过程和触发器可以进行事务管理. 二.编程基础介绍 主要有:函数.程序设计语句等. 程序设计语句: ...
- ie 浏览器无法保存cookie,且与域名包括了下划线(_)有关系的问题
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- win2012R2无法打开匿名级安全令牌
解决办法: 1.输入“dcomcnfg.exe”,打开组件服务管理. 2.展开组件服务,计算机,右击我的电脑,选择属性. 3.在默认属性选项卡中, 选择:- 勾选“在此计算机中启用分布式C ...