1029 Median (25 point(s))

JAVA:实现求Median中位数算法(附完整源码) 阅读详情

1029 Median (25 point(s))

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 Specification:

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 (≤2×10​5​​) 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 Specification:

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

 

思路:

1.原本思路:开两个数组,然后采用归并排序的方式查找median元素(设置两个游标i,j)。结果:内存超限(22)。

2.正解思路:只开一个数组,然后对于每一个新输入的元素进行判断。

3.核心:归并排序

(1)注意判断median元素(即cnt==0时结束);

(2)注意有可能第二个序列输入完以后,还没有找到median(即在数组a[]还没遍历的部分)。

#include<iostream>
using namespace std;
const int MAX = 200007;
int a[MAX];
int main(void)
{
	int n,m;
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	scanf("%d",&m);
	int temp;
	int index=0;
	int cnt = (n+m+1)/2;//目标计数 
	int res;
	for(int i=0;i<m;i++){
		scanf("%d",&temp);
		while(temp>a[index]&&index<n&&cnt){//如果新输入的数大于当前index指向的数 
			index++;cnt--;//index移动,计数减少1 
		}
		if(cnt==0){//如果此时计数结束,则说明中位数已经产生 
			res=a[--index];//注意必须要回溯1个元素 
			break;
		}
		cnt--;//没有到达中位数位置(即cnt不是0),则计数再减少1个(即temp) 
		if(cnt==0){//如果此时计数结束,原理同上 
			res=temp;
			break;
		}
	}
	if(cnt){//如果所有的数输入完毕以后,还没有出现中位数,说明中位数在序列a[]中 
		while(cnt--){
			index++;
		}
		res=a[--index];
		//res=a[index+cnt-1]; 
	}
	printf("%d\n",res);
	return 0;
}

附:原本思路代码

#include<iostream>
using namespace std;
const int MAX = 200007;
int a[MAX],b[MAX];
int main(void)
{
	int n,m;
	scanf("%d",&n);
	for(int i=0;i<n;i++) scanf("%d",&a[i]);
	scanf("%d",&m);
	for(int i=0;i<m;i++) scanf("%d",&b[i]);
	int count=0,i=0,j=0;
	int pre;
	while(count<(n+m+1)/2){
		if(i<n&&j<m){
			if(a[i]<b[j]){
				pre = a[i];
				i++;
			} 
			else{
				pre = b[j];
				j++;
			}
		}
		else if(i>=n){
			pre = b[j];
			j++;
		}
		else{
			pre = a[i];
			i++;
		}
		count++;
	}
	printf("%d\n",pre);
	return 0;
}

https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968

 

全球APN信息汇总 全球APN信息,电信APN,移动APN,联通APN,台湾APN,马来西亚APN,俄罗斯APN,日本APN,泰国APN,美国APN,英国APN,法国APN,德国APN,意大利APN,葡萄牙APN,土耳其APN,荷兰APN,丹麦APN,比利时APN,爱尔兰APN,新加坡APN,越南APN,老挝APN,菲律宾APN,印尼APN,泰国APN,柬埔寨APN,巴基斯坦APN,印度APN,奥地利APN,保加利亚APN,捷克APN,立陶宛APN,罗马尼亚APN,挪威APN,希腊APN,匈牙利APN,瑞士APN,乌克兰APN 阅读详情

相关推荐

银河麒麟V10离线安装VLC播放器:依赖处理与3.0.9.2版本实操指南

在国产化操作系统日益普及的背景下,软件安装方式成为政企内网用户关注的重点。离线安装作为一种不依赖公网源的部署手段,常用于解决内网环境下的软件获取问题。其核心在于理解Linux包管理机制,如dpkg与apt对依赖关系的处理逻辑。掌握这一原理,不仅能应对多媒体软件的部署,也能提升系统维护效率。VLC作为跨平台多媒体播放器,凭借强大的解码能力与协议支持,广泛应用于本地视频播放、RTSP拉流及监控大屏等场景。本文从离线安装的通用概念出发,结合银河麒麟V10桌面版的实际环境,详细梳理了VLC 3.0.9.2离线包的获

weixin_32247165的博客 375

算法:寻找两个正序数组的中位数。

给定两个大小为 m 和 n 的正序(从小到大)数组nums1 和nums2。 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为O(log(m + n))。

孤芳不自赏 9688

VFIO框架源码分析(二十二)- IOMMUFD 的脏页跟踪与 Dirty Bits 读取

在虚拟化热迁移(Live Migration)的场景中,脏页追踪(Dirty Page Tracking)是最具挑战性的环节之一。它的核心任务是实时捕获设备(DMA)对内存的写操作,以便 VMM(如 QEMU)知道哪些内存页需要被重新传输到目标主机。在强耦合与粗粒度:脏页追踪是绑定在 Container 上的。这意味着,如果一个 Container 内有多个设备,哪怕只有一个设备在写内存,脏页追踪也会影响 Container 内的所有设备。软件模拟开销大。

qq_36071928的博客 1046

LeetCode-C#-0004.寻找两个正序数组的中位数

该题目来源于如有侵权,立马删除。解法不唯一,如有新解法可一同讨论。

mosi357的博客 994

【TM1650】STM8S TM1650芯片控制程序

以下为.h文件: #ifndef __TM1650_I2C_H #define __TM1650_I2C_H #include "stm8s.h" #include "stm8s_gpio.h" #include "tim1.h" #include "usart2.h" #include &lt;iostm8s103f3.h&gt; #include &lt;intrinsics....

cxw312864660的博客 1万+

74、光谱成像在电子内窥镜及色彩理论中的应用

本文探讨了光谱成像在电子内窥镜中的应用及其与色彩理论的深度融合。通过分析FICE技术、色彩再现模型及胃黏膜光谱测量,展示了光谱信息在提升医学诊断准确性方面的潜力。同时,系统阐述了颜色的物理属性、标准表示方法、同色异谱现象、颜色测量技术(密度法与比色法)以及CIELAB等均匀颜色空间的构建原理。结合流程图与对比表格,梳理了从光信号获取到颜色处理的完整路径,并展望了人工智能与新型视觉模型在未来医疗成像与色彩科学中的发展方向。

g2h3i4j5的博客 153

如何找到两个数组的中位数

什么意思呢?让我们来看两个例子: 上图这两个给定数组A和B,一个长度是6,一个长度是5,归并之后的大数组仍然要保持升序,结果如下: 大数组的长度是奇数(11),中位数显然是位于正中的第6个元素,也就是元素5 上面的例子是奇数个元素的情况。那么偶数的元素是什么样呢?让我们来看另一个例子: 上图这两个给定数组A和B,长度都是5,归并之后的大数组如下: 大数组的长度是偶数(10),位于正中的元...

WinstonLau的博客 3768

【TC3xx芯片】TC3xx芯片STM介绍

TC3xx芯片STM介绍

qq_36056498的博客 2280

两数组中位数

leetcode 4----两数组中位数 解法(一) 通过新建数组的办法,将两个数组合并成一个,然后找中位数,代码容易理解,但不是很快并且有点浪费空间。 public double findMedianSortedArrays(int[] nums1, int[] nums2) { int len = nums1.length+nums2.length; int i = 0,j = 0,k=0; while(i<nums1.length &&am

qq_43203949的博客 712

uni-app - 将 base64 图片编码转为 Blob 本地文件路径(把base64类型的图片,转换成blob二进制文件流)适用于 uni-app / vue.js / JavaScript

uni-app/vue.js/JavaScript将base64转Blob,uniappapp端如何将图片的base64编码转换成File对象或Blob,uni-app/vue.js/JavaScript中拿到base64转blob对象,uniapp图片base64格式转blob格式方法,uniapp上传图片压缩转base64码与base64转图片路径,Base64/Blob/File图片文件转换,url、base64、blob相互转换方法,读取本地文件为Base64并转成B

🏆 前端领域优质创作者 1万+

rk3568 修改开机动画

bootanimation.zip是一个Android设备启动时播放的动画文件,它包含了一系列的图片和描述文件,用于描述动画的播放顺序和持续时间。

weixin_35723192的博客 2729

1029 Median (25point(s))

1029 Median (25point(s)) 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 sequence

佳迪斯的博客 126

【K8S系列】在 Kubernetes 中使用 Prometheus 进行监控的详细指南

Prometheus 是一个开源的监控和报警工具,广泛用于 Kubernetes 环境中。本文将深入探讨如何通过 Kubernetes 中的注解配置 Prometheus 的抓取设置,以高效监控服务,并确保系统的可靠性和可维护性。

颜淡慕潇 2万+

1029 Median (25 point(s))

 题解 题意不难,难点在于内存控制,可以在读取第二行数据时,边读取边运算。 #include&lt;cstdio&gt; using namespace std; const int MAXN = 2e5 + 10; int n, m, t; int v[MAXN]; int main() { scanf("%d", &amp;n); for(int i = 1; i &lt;= n...

徐伯莱 293

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

1029 Median (25 point(s))

思路: 计算的出中位数的位置,然后比较两个数组,得出中位数,时间复杂度为 O(n+m) 1029 Median (25 point(s)) 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

专栏 158

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

PAT-A 1029 Median (25 point(s))

思路:内存限制1.5M,如果两个数组的长度都是2×10​^5,那么刚好会超出1.5M,导致卡一个点,因此第二个数组不能一次性输入后再进行计算。每输入第二个数组中的一个数字,进行判断。如果第二个数组中的数字完全输入后还是没有正确答案,那么中位数就在第一个数组中。

qq_38127801的博客 297

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

PTA甲级 1029 Median (25point(s))

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬 本文由参考于柳神博客写成 柳神的优快云博客,这个可以搜索文章 柳神的个人博客,这个没有广告,但是不能搜索 还有就是非常非常有用的 算法笔记 全名是 算法笔记 上级训练实战指南 //这本都是PTA的题解 算法笔记 PS 今天也要加油鸭 题目原文 Given an increasing sequence S of N integers, the median is the number at the middle position. For ex

Huangpengyu123的博客 249

1029 Median (25 point(s)) - C语言 PAT 甲级

设计思路: 1. 两个序列都存下来,会超过题目要求的内存限制 2. 储存第一个序列,第二个序列不储存,在输入的过程中寻找中间数;序列分别有 n1 和 n2 个数,则中间数在 (n + m + 1) / 2 的位置 3. 序列后面加一个最大值 INT_MAX 当“哨兵”,可以简化处理过程

有三只土豆 201

PAT A1029 Median (25point(s))

题目链接 #include<cstdio> #include<algorithm> using namespace std; long long test[1000000]; //存放两个数组合并之后的序列 int main(){ int i,n1,n2; scanf("%d",&n1); for(i=0;i<n1;i++){//输入第...

皮皮瓜的博客 93

PAT A1029 Median (25 point(s)) __two pointers

题目大意: 给出两个有序数组,要求输出两个数组合并之后的中位数。 思路: 正常思路是按照merge的思想,mid=(m+n-1)/2 (数组从0开始),边merge边判断当前count是否==mid 但本题内存限制比较严格,上面思路会有一个测试点超内存。所以可以采用双指针法,只输入第一个数组,边输入第二个数组边与第一个数组比较,时刻注意count的取值。 代码1(有一个测试点内存超载):...

a7701935的博客 266

PAT (Advanced Level) Practice 1029 Median (25 point(s))

题目PAT A 1029 题目大意 给两个有序的序列,求这两个序列合并以后的中位数 题解思路 方法一:归并排序 方法二:二分(待更新) 方法一:归并排序 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+5; ll a[maxn],b[maxn]; //使用归并排序 ll c[maxn<<1]; ll merge(int n,int m){ int c

helloful的博客 141

1029 Median(25 point(s)(cj)

1029 Median(25 point(s)) 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 ...

Cute_jinx的博客 466

1029.Median (25)

1029.Median (25)pat-al-10292017-02-23 利用哨兵 数组太大不能放main里 /** * pat-al-1029 * 2017-02-23 * C version * Author: fengLian_s */ #include<stdio.h> #define INF 0x3f3f3f3f #define MAX 1000010 long num1[MA

一剑钦安 351

1029 Median(25 分)

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...

码奴生来就只知道前进 585

1029 Median (25 分)(two pointer)

1029 Median (25 分)(two pointer) 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 ...

virgilshi的博客 402

PAT-1029 Median (25)

题目大意:给定两个有序串,长为 n,m。问两者合并的中位数是哪个。解题思路:本题采用的 类插入排序 ,用游标记录两个数组的比较前端,到达 (m+n)/2 停止。时间复杂度:O(max(n,m)) 。题目链接:https://www.patest.cn/contests/pat-a-practise/1029#include &lt;iostream&gt; #inclu...

zhoujian_1943的博客 460
上一篇: 【机试】ProblemC(整数的最大素因子)
下一篇: 【机试】单词识别
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值