题目1004:Median

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:17536

解决:4860

题目描述:                       

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

此题要求输出两个递增数列并到一起后的中间数,代码如下
 #include <stdio.h>
#define MAX 1000002
long int A[MAX];
long int B[MAX];
int main() {
int M,N;
while(scanf("%d",&M) != EOF) {
for(int i = ; i < M; i++) {
scanf("%ld",&A[i]);
}
scanf("%d",&N);
for(int i = ; i < N; i++) {
scanf("%ld",&B[i]);
}
int middle = (M+N-)/;
int a = , b =, i = ;
int flag = ;
while(i <= middle) {
if(A[a] < B[b] && a < M && b < N) {
a++;
i++;
flag = ;
//printf("%d\n",A[a]);
continue;
}
else if(A[a] > B[b] && a < M && b < N) {
b++;
i++;
flag = ;
//printf("%d\n",B[b]);
continue;
}
else if(a < M && b < N){
a++;
b++;
i++;
i++;
flag = ;
//printf("%d\n",A[a]);
continue;
}
else if(a < M && b >= N) {
a++;
i++;
flag = ;
//printf("%d\n",A[a]);
continue;
}
else if(a >= M && b < N) {
b++;
i++;
flag = ;
//printf("%d\n",B[b]);
continue;
}
}
if(flag == ) {
printf("%ld\n", A[a-]);
}
else {
printf("%ld\n", B[b-]);
}
}
return ;
}

九度oj 1004的更多相关文章

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

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

  2. 九度OJ 1004:Median

    #include <stdio.h> #include <stdlib.h> #include <limits.h> #define N 1000000 int a ...

  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. window对象的几个重要方法

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

  2. 2017.10.7 QBXT 模拟赛

    题目链接 T1 容斥原理,根据奇偶性进行加减 #include<iostream> #include<cstdio> using namespace std; typedef ...

  3. [学习笔记] AD笔记

    Auto diff 深度学习基础知识,auto diff自动微分的笔记,tensorflow中的求导就是基于这个做的.多用于复杂神经网络求导.来自于一篇论文,没怎么看完,但是会算了,比较底层一点吧.. ...

  4. Kubernetes之pod的属性

    属性名称 取值类型                   是否必选 取值说明 version String Required(必) 版本号,例如v1 kind String Required pod m ...

  5. Fruit Ninja(取随机数)

    链接:https://www.nowcoder.com/acm/contest/163/A来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C++ 262144K,其他语言524 ...

  6. 解决ubuntu上ifconfig没有eth0/ens33且无法上网的问题

    ifconfig只有一个轮回端口lo,没有我们的网卡eth0,一开始以为是vsphere(新手对于vsphere不是很熟悉)上我的虚拟机配置问题,还查看了相关的网络配置,后来才知道是因为: 问题出在配 ...

  7. 作业题:输出单个字符 输入单个字符 scanf printf

    输出单个字符用putchar() #include <iostream> using namespace std; int main(){ char x='B'; char y='O'; ...

  8. 解决cocos simpleAudioEngine播放mp3失败问题

    今天用cocos3.x版本实现游戏音乐播放发现一个坑,策划发来的mp3格式音频,用 simpleAudioEngine无法播放, 以为是路径问题,断点调试没找到,然后拷贝了cocos自带的mp3音频文 ...

  9. PostgreSQL学习(2)-- mvcc

    1.PG事务隔离级别 在数据库中,并发的操作进行读写数据时,则会遇到脏读.不可重复读.幻读.串行化异常等问题. 数据库事务的特性: 原子性(Atomicity):事务作为一个整体被执行,包含在其中的对 ...

  10. Bzoj 近期题目一句话题解

    目录 Bzoj 近期题目题解 1000: A+B Problem (模拟) 1008: [HNOI2008]越狱 (容斥) 1012: [JSOI2008]最大数maxnumber (线段树) 103 ...