九度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 ...
随机推荐
- (4)python tkinter-布局
包装布局pack() 目前对它的感觉是,当一个窗体的对象都设置完属性后,最后用它来绑定到窗体上.之后就不能再设置属性了 名称 描述 取值范围 expand 当值为“yes”时,side选项无效.组件显 ...
- Codeforces 916E Jamie and Tree (换根讨论)
题目链接 Jamie and Tree 题意 给定一棵树,现在有下列操作: $1$.把当前的根换成$v$:$2$.找到最小的同时包含$u$和$v$的子树,然后把这棵子树里面的所有点的值加$x$: ...
- javascript解析机制——预解析
JavaScript解析机制是什么? JavaScript解析过程分为两个阶段,一个是编译阶段,另外一个就是执行阶段. * 编译阶段 编译阶段就是我们常说的JavaScript预解析( ...
- SQLServer出现不允许保存更改的问题解决
如图所示: 解决方法: [工具]->[选项]
- 优化算法——拟牛顿法之L-BFGS算法
一.BFGS算法 在"优化算法--拟牛顿法之BFGS算法"中,我们得到了BFGS算法的校正公式: 利用Sherman-Morrison公式可对上式进行变换,得到 令,则得到: 二. ...
- AutoCAD如何输入文字
1 运行文字命令(这里使用单行文字),然后鼠标点击文字的起始点,如图所示 2 鼠标分别向上和向右移动一定距离,表示文字的高度(文字的大小)和文字的旋转角度(一般向右,因为是水平文字) 3 最后 ...
- HBase1.0以上版本号的API改变
HBase1.0以上版本号已经废弃了 HTableInterface,HTable,HBaseAdmin等API的使用.新增了一些API来实现之前的功能: Connectioninterface: C ...
- 一波三折ST-Link
前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的链接.图片显示,请访问我的博客原文: http://www.cnblog ...
- Intellij IDEA 的使用
前言: 为什么我要选择intellij idea呢?原因有三 1.曾经公司的项目 在myeclipse10中不稳定 myeclipse说不定什么时候就崩溃卡死了 而intellij非常稳定 2.界面 ...
- java查看工具jinfo-windows
Generates configuration information. This command is experimental and unsupported. Synopsis jinfo [ ...