热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

题目1004:Median(数组Merge)

题目1004:Median时间限制:1秒内存限制:32兆特殊判题:否提交:1716解决:432题目描述:GivenanincreasingsequenceSofNintege

题目1004:Median

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:1716

解决:432

题目描述:

    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 "iostream"
#include "stdio.h"
#include "math.h"
#include "map"
#include "vector"
#include "queue"
#include "memory.h"
#include "algorithm"
#include "string"
using namespace std;
#define N 1000005
#define INF 1<<29
#define max(a,b) a>b?a:b

int a[N],b[N];
int array3[N];

int MergeArray(int la,int ha,int lb,int hb)
{
int i,j,x;
i=la,j=lb,x=0;
while(i<=ha&&j<=hb)
{
if(a[i]>b[j]) array3[x]=b[j++];
else array3[x]=a[i++];
x++;
}
while(i<=ha)array3[x++]=a[i++];
while(j<=hb)array3[x++]=b[j++];
x--;
return array3[x/2];
}

int main()
{
int m,n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=0;iscanf("%d",&a[i]);
cin>>m;
for(j=0;jscanf("%d",&b[j]);
int la,lb,ha,hb,ma,mb;
la=lb=0;ha=n-1;hb=m-1;
int res;
int len=1;
while(len>0)
{
ma=(la+ha)>>1;mb=(lb+hb)>>1;
if(a[ma]==b[mb])
{
res=a[ma];
break;
}
if(a[ma]>b[mb])
{
len=min(ha-ma,mb-lb);
ha-=len;
lb+=len;
}
else if(a[ma]{
len=min(ma-la,hb-mb);
la+=len;
hb-=len;
}
}
if(a[ma]==b[mb])
{
printf("%d\n",res);
continue;
}
res=MergeArray(la,ha,lb,hb);
printf("%d\n",res);
}
}

或者,其实这道题目不进行main中的那个优化也行的。
#include "iostream"
#include "stdio.h"
#include "math.h"
#include "map"
#include "vector"
#include "queue"
#include "memory.h"
#include "algorithm"
#include "string"
using namespace std;
#define N 1000005
#define INF 1<<29
#define max(a,b) a>b?a:b

int a[N],b[N];
int array3[N];

int MergeArray(int la,int ha,int lb,int hb)
{
int i,j,x;
i=la,j=lb,x=0;
while(i<=ha&&j<=hb)
{
if(a[i]>b[j]) array3[x]=b[j++];
else array3[x]=a[i++];
x++;
}
while(i<=ha)array3[x++]=a[i++];
while(j<=hb)array3[x++]=b[j++];
x--;
return array3[x/2];
}

int main()
{
int m,n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=0;iscanf("%d",&a[i]);
cin>>m;
for(j=0;jscanf("%d",&b[j]);
int la,lb,ha,hb,ma,mb;
la=lb=0;ha=n-1;hb=m-1;
int res;

res=MergeArray(la,ha,lb,hb);
printf("%d\n",res);
}
}


解法2:(利用algorithm 中的nth_element())

#include "iostream"
#include "stdio.h"
#include "math.h"
#include "map"
#include "vector"
#include "queue"
#include "memory.h"
#include "algorithm"
#include "string"
using namespace std;
#define N 1000005
#define INF 1<<29
#define max(a,b) a>b?a:b

long a[1000000];
int main()
{
long n1,n2,i,mid;
while(cin>>n1)
{
for(i=0;i cin>>a[i];
cin>>n2;
for(;i cin>>a[i];
mid=(i-1)/2;
nth_element(a,a+mid,a+i);
cout< }
return 0;
}







推荐阅读
  • Parity game(poj1733)题解及思路分析
    本文是对题目"Parity game(poj1733)"的解题思路进行分析。题目要求判断每次给出的区间内1的个数是否和之前的询问相冲突,如果冲突则结束。本文首先介绍了离线算法的思路,然后详细解释了带权并查集的基本操作。同时,本文还对异或运算进行了学习,并给出了具体的操作步骤。最后,本文给出了完整的代码实现,并进行了测试。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 本文介绍了如何使用MATLAB调用摄像头进行人脸检测和识别。首先需要安装扩展工具,并下载安装OS Generic Video Interface。然后使用MATLAB的机器视觉工具箱中的VJ算法进行人脸检测,可以直接调用CascadeObjectDetector函数进行检测。同时还介绍了如何调用摄像头进行人脸识别,并对每一帧图像进行识别。最后,给出了一些相关的参考资料和实例。 ... [详细]
  • 【每天三题】day 6.8
    题解参考:环形链表是否有环,求入环节点长度,求形成环的长度合并两个链表合并K个链表publicListNodemergeKLists ... [详细]
  • 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?Input测 ... [详细]
  • 湍流|低频_youcans 的 OpenCV 例程 200 篇106. 退化图像的逆滤波
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了youcans的OpenCV例程200篇106.退化图像的逆滤波相关的知识,希望对你有一定的参考价值。 ... [详细]
  • ProblemDescriptionXiaoAlivesinavillage.Lastyearfloodrainedthevillage ... [详细]
  • 一、MyEclipse中的一些常用的快捷键:ctrl+shift+x大写ctrl+shift+y小写alt+内容提示写住方法的时候可以先写main然后按alt+就可以了ctrl+1 ... [详细]
  • Imtryingtomakeawebsiteinwhichauserinputsdetailsononescreen,andtheyarepostedonto ... [详细]
  • 题目大意:给定数字,将其转化为罗马数字的形式罗马数字其实只有IVXLCDM这几种形式,其余均为组合的,去百度了解一下就ok。所以首先想到的就是,将个、十、百、千位的 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
author-avatar
hhqblog
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有