博客
关于我
PTA 7-3 两个有序序列的中位数 (25 分)
阅读量:739 次
发布时间:2019-03-22

本文共 787 字,大约阅读时间需要 2 分钟。

已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A​0​​,A​1​​,⋯,A​N−1​​的中位数指A​(N−1)/2​​的值,即第⌊(N+1)/2⌋个数(A​0​​为第1个数)。

输入格式:

输入分三行。第一行给出序列的公共长度N(0<N≤100000),随后每行输入一个序列的信息,即N个非降序排列的整数。数字用空格间隔。

输出格式:

在一行中输出两个输入序列的并集序列的中位数。

输入样例1:

51 3 5 7 92 3 4 5 6

输出样例1:

4

输入样例2:

6-100 -10 1 1 1 1-50 0 2 3 4 5

输出样例2:

1
#include 
#define LIST_SIZE 100000int S1[LIST_SIZE],S2[LIST_SIZE];//直接求索引int Dealwith(n){ int i = 0,j = 0; int m = (2*n-1)/2; while(i + j < m) { if(S1[i] >= S2[j]) { j++; } else i++; } return S1[i]>S2[j]?S2[j]:S1[i];}int main(){ int n; scanf("%d",&n); int i; for(i = 0;i < n;i++) { scanf("%d",&S1[i]); } for(i = 0;i < n;i++) { scanf("%d",&S2[i]); } printf("%d\n",Dealwith(n)); return 0;}

 

转载地址:http://bkbwk.baihongyu.com/

你可能感兴趣的文章
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>