CSU 1212 中位数
来源:互联网 发布:甬商贷网络借贷 编辑:程序博客网 时间:2024/06/11 23:46
1212: 中位数
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 433 Solved: 178
[Submit][Status][Web Board]
Description
长为L的升序序列S,S[L / 2]为其中位数。
给出两个等长升序序列S1和S2,求两序列合并并排序后的中位数。
Input
多组数据,每组第一行为n,表示两个等长升序序列的长度。
接下来n行为升序序列S1的元素,再接下来n行为升序序列S2的元素。
1 <= n <= 10 ^ 5,S内容为整数。
不超过5组数据。
Output
每组数据,输出合并并排序后的序列的中位数。
Sample Input
51113151719246820
Sample Output
11
HINT
水题。设一个flag可以让程序找到结果后提前跳出去,减少时间。
#include <stdio.h>const int MAXN=100005;int numsA[MAXN],numsB[MAXN];int numsC[MAXN];int main(){ int n; while(scanf("%d",&n)>0) { for(int i=1;i<=n;i++) scanf("%d",&numsA[i]); for(int i=1;i<=n;i++) scanf("%d",&numsB[i]); int flag=0; int pa=1,pb=1,pc=1; while(pa<=n&&pb<=n) { if(numsA[pa]<=numsB[pb]) { numsC[pc]=numsA[pa]; pa++; } else { numsC[pc]=numsB[pb]; pb++; } if(pc==n) { printf("%d\n",numsC[pc]); flag=1; break; } pc++; } while(pa<=n&&flag==0) { numsC[pc]=numsA[pa++]; if(pc==n/2) { printf("%d\n",numsC[pc]); flag=1; break; } pc++; } while(pb<=n&&flag==0) { numsC[pc]=numsB[pb++]; if(pc==n/2) { printf("%d\n",numsC[pc]); break; } pc++; } } return 0;} /************************************************************** Problem: 1212 User: 0905130123 Language: C++ Result: Accepted Time:116 ms Memory:2136 kb****************************************************************/
0 0
- CSU 1212 中位数
- CSU - 1212 中位数
- CSU-1212-中位数
- csu:1947: 突然的灵光 +csu:1952: 地下情报员 (中位数)
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- androidstudio 打包签名问题
- VMware10中的Linux系统利用NAT网络连接方式访问外网配置
- 设计模式:原型模式(Prototype)
- linux下利用线程池构建高并发server
- [学习笔记]Java网络编程之TCP通讯
- CSU 1212 中位数
- [学习笔记]Java正则表达式
- CentOS6.5安装(硬盘空间大于16T)
- HDU 1028 Ignatius and the Princess III(母函数或dp)
- Google Code Jam 2016 Round 1A
- 2016SDAU编程练习二1005
- IPC机制———读书笔记
- test
- mysql中的当前时间函数