九度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 ...
随机推荐
- Codeforces Gym100971 F.Two Points (IX Samara Regional Intercollegiate Programming Contest Russia, Samara, March 13)
这个题直接推公式就可以. 就是解一元二次方程,用高中学的公式,函数开口向上,求最大值为(4ac-b*b)/4a. 这个题推出来一元二次方程,然后将最大值的公式化简一下.公式很好推. 这个题有疑问,in ...
- HDU 5242 Game(树上贪心)
题目链接 Game 题目的意思很简单, 就是要找一棵树权值最大等等前K条链. 在本题中,走的次数等于min(叶子结点个数,k) tree[i].sum意为从i号结点出发走到某个叶子结点能得到的最大总价 ...
- Concurrency(Locking, Blocking and Row Versioning)
https://www.simple-talk.com/sql/t-sql-programming/row-versioning-concurrency-in-sql-server/?utm_sour ...
- pt-online-schema-change原理解析(转)
pt-online-schema-change原理解析 博客相关需要阅读 - zengkefu - 博客园 .pt-online-schema-change工具的使用限制: ).如果修改表有外键,除非 ...
- [资料分享]GIS+=地理信息+云计算+大数据+容器+物联网+...论文、会议、讲座资料分享
分享地址 http://pan.baidu.com/s/1gesDSB5 部分内容截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...
- Activity入门(一)
生命周期 onCreate():activity进行创建,在该方法中应调用setContentView(),findViewById()以及获取要展示的数据的方法(如调用manager ...
- UNP学习笔记(第二章:传输层)
本章的焦点是传输层,包括TCP.UDP和SCTP. 绝大多数客户/服务器网络应用使用TCP或UDP.SCTP是一个较新的协议. UDP是一个简单的.不可靠的数据报协议.而TCP是一个复杂.可靠的字节流 ...
- c语言字符数组的初始化问题
1.字符数组的定义与初始化 字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素. char str[10]={ 'I',' ','a','m',' ',‘h’,'a','p','p','y ...
- CSS - 修改input - placeholder 和 readonly 的样式
placeholder ::-webkit-input-placeholder { /* WebKit browsers */ color: #999999; } :-moz-placeholder ...
- PHP session回收机制(转)
由于PHP的工作机制,它并没有一个daemon线程,来定时地扫描session信息并判断其是否失效.当一个有效请求发生时,PHP会根据全局变量 session.gc_probability/sessi ...