A1029. Median
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 nondecreasing 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.
Input
Each input file contains 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.
Output
For each test case you should output the median of the two given sequences in a line.
Sample Input
4 11 12 13 14
5 9 10 15 16 17
Sample Output
13
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int num1[], num2[];
int main(){
int N1, N2;
scanf("%d", &N1);
for(int i = ; i < N1; i++){
scanf("%d", &num1[i]);
}
scanf("%d", &N2);
for(int i = ; i < N2; i++){
scanf("%d", &num2[i]);
}
int index = -, ans = , i = , j = , mid = (N1 + N2 - ) / ;
while(i < N1 && j < N2 && index != mid){
if(num1[i] < num2[j])
ans = num1[i++];
else ans = num2[j++];
index++;
}
while(i < N1 && index != mid){
ans = num1[i++];
index++;
}
while(j < N2 && index != mid){
ans = num2[j++];
index++;
}
printf("%d", ans);
cin >> N1;
return ;
}
A1029. Median的更多相关文章
- PAT A1029 Median (25 分)——队列
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- A1029 Median (25 分)
一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...
- PAT甲级——A1029 Median
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- No.004:Median of Two Sorted Arrays
问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- [LeetCode] Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Applying vector median filter on RGB image based on matlab
前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...
- 【leetcode】Median of Two Sorted Arrays
题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
随机推荐
- Quartz.net 定时任务之储存与持久化和集群(源码)
一.界面 1.这篇博客不上教程.直接看结果(包括把quartz任务转换成Windows服务) (1).主界面 (2).添加任务(默认执行) (3).编辑(默认开启) (4).关闭和开启 2.代码说明 ...
- 微信小程序开发工具 ubuntu linux版本
安装 http://blog.csdn.net/zhangyingguangails/article/details/72517182 sudo apt install wine sudo git c ...
- ubuntu系统升级和其他相关操作记录
之前在openstack中安装了ubuntu 12.04虚拟机,版本较低,需要升级为高版本.下面分享下升级过程: ubuntu系统升级操作:$ cat /etc/issueUbuntu 12.04.5 ...
- 状态模式-State-订单状态
JAVA设计模式-状态模式-State-订单状态 21. State(状态) 意图: 允许一个对象在其内部状态改变时改变它的行为.对象看起来似乎修改了它的类. 解释: 比如说对订单的提交,第一 ...
- 读《移山之道——VSTS软件开发指南》
读<移山之道>这本书差不多用了一个星期的时间,感觉还是收获了一些知识的,以前只是会简单地编个小程序(虽然现在也是这样),但看过这本书之后我对软件开发这个概念的认识度有了从一片模糊到了解大体 ...
- Linux实验四报告
张文俊 + 原创作品转载请注明出处+ <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.学习内容 系统 ...
- mapreduce 中 map数量与文件大小的关系
学习mapreduce过程中, map第一个阶段是从hdfs 中获取文件的并进行切片,我自己在好奇map的启动的数量和文件的大小有什么关系,进过学习得知map的数量和文件切片的数量有关系,那文件的大小 ...
- Eclipse使用Maven2的一次环境清理记录
1. C:\Users\Administrator\.m2\repository\com\yuanchuangyun\[module,yuanchuangyun-*]相关目录全删除.2. D:\wor ...
- Character Encoding Issues for tomcat
https://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8 https://stackoverflow.com/questions/10936846 ...
- C1考试科目一知识总结
第二 交通信号 交通信号灯 机动车信号灯(红灯停,路灯走,黄灯等) 车道信号灯(绿色箭头表示该车道通行,红色箭头和红叉表示该车道禁止通行) 方向指示信号灯(红色箭头表示该方向禁止通行,绿色箭头表示该方 ...