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 ...
随机推荐
- c#基础系列3---深入理解ref 和out
"大菜":源于自己刚踏入猿途混沌时起,自我感觉不是一般的菜,因而得名"大菜",于自身共勉. 扩展阅读 c#基础系列1---深入理解 值类型和引用类型 c#基础系 ...
- 个人阅读作业Week5
一.总结体会 团队项目已经进行了很多周,我们团队从刚开始的基础薄弱到现在的大家都可以运用Android来编写程序,共同完成一个app的开发使用. 刚开始做团队项目之时,我们团队就开了一个会,确定了以后 ...
- 第八周--Linux中进程调度与进程切换的过程
[潘恒 原创作品转载请注明出处 <Linux内核分析>MOOC课程 "http://mooc.study.163.com/course/USTC 1000029000 " ...
- Linux内核分析— —操作系统是如何工作的(20135213林涵锦)
mykernel实验指导(操作系统是如何工作的) 实验要求 运行并分析一个精简的操作系统内核,理解操作系统是如何工作的 使用实验楼的虚拟机打开shell cd LinuxKernel/linux-3. ...
- 基于SSH框架开发的《高校大学生选课系统》的质量属性的实现
基于SSH框架开发的<高校大学生选课系统>的质量属性的实现 对于可用性采取的是错误预防战术,即阻止错误演变为故障:在本系统主要体现在以下两个方面:(1)对于学生登录模块,由于初次登陆,学生 ...
- 团队作业:SRS文档-飞机大战
本实验为团队合作项目作业的一部分:SRS文档-飞机大战 项目分工:SRS文档项目为梁JM负责完成 实验要求: 3.SRS文档(第二周,截止5月31日) 要求对所选项目进行用例 ...
- JavaScript 编程易错点整理
Case 1: 通过getElementById("id")获得是一个DOM元素节点对象: 通过getElementsByTagName("tagName")获 ...
- PAT 1038 统计同成绩学生
https://pintia.cn/problem-sets/994805260223102976/problems/994805284092887040 本题要求读入N名学生的成绩,将获得某一给定分 ...
- SQLServer:介质簇计数 缺失的介质簇序列号
https://shiyousan.com/post/635886596017415485 http://www.cnblogs.com/yc-755909659/p/3725940.html 错误描 ...
- Install alipay支付宝安全控件 on firefox in linux
[root@rgqancy 下载]# ./aliedit.sh建议以非root账号安装支付宝安全控件请重启 firefox 使插件生效成功安装 支付宝安全控件请按任意键退出... what i ...