1029. Median (25)
2015-08-12 09:30
561 查看
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
Sample Output
提交代码
——————————
在leetcode上能找到类似的题目,幸好自己曾经刷过。
找到第K小数。
假设AB中数大于K/2个,比较A[K/2-1]和B[K/2-1]的大小。
如果,A[k/2-1]<B[k/2-1],说明A[0]~A[k/2-1]在合并后都比前K小,可以舍弃。
同理。
下面的写法总是出现问题,主要的原因是因为数组分配的问题,换成指针之后就可以了。
AC————————
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
提交代码
——————————
在leetcode上能找到类似的题目,幸好自己曾经刷过。
找到第K小数。
假设AB中数大于K/2个,比较A[K/2-1]和B[K/2-1]的大小。
如果,A[k/2-1]<B[k/2-1],说明A[0]~A[k/2-1]在合并后都比前K小,可以舍弃。
同理。
下面的写法总是出现问题,主要的原因是因为数组分配的问题,换成指针之后就可以了。
#include <iostream> #include <cstring> #include <vector> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long int findkth(long long int a[], long long int m, long long int b[], long long int n, long long int k){ if(m>n) return findkth(b,n,a,m,k); if(m==0) return b[k-1]; if(k==1) return min(a[0],b[0]); long long int pa=min(k/2,m),pb=k-pa; if(a[pa-1]<b[pb-1]) return findkth(a+pa,m-pa,b,n,k-pa); else if(a[pa-1]>b[pb-1]) return findkth(a,m,b+pb,n-pb,k-pb); else return a[pa-1]; } int main(int argc, char** argv) { long long int n1,n2; long long int a[10010]={0},b[10010]={0}; scanf("%lld",&n1); for(long long int i=0; i<n1; i++){ scanf("%lld",&a[i]); } scanf("%lld",&n2); for(long long int i=0; i<n2; i++){ scanf("%lld",&b[i]); } long long int total=n1+n2; long long int res=0; if(total&0x1) res=findkth(a,n1,b,n2,total/2+1); else res=findkth(a,n1,b,n2,total/2); printf("%lld\n",res); return 0; }
AC————————
#include <iostream> #include <cstring> #include <vector> #include <stdlib.h> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ long long int findkth(long long int a[], long long int m, long long int b[], long long int n, long long int k){ if(m>n) return findkth(b,n,a,m,k); if(m==0) return b[k-1]; if(k==1) return min(a[0],b[0]); long long int pa=min(k/2,m),pb=k-pa; if(a[pa-1]<b[pb-1]) return findkth(a+pa,m-pa,b,n,k-pa); else if(a[pa-1]>b[pb-1]) return findkth(a,m,b+pb,n-pb,k-pb); else return a[pa-1]; } int main(int argc, char** argv) { long long int n1,n2; long long int *a,*b; scanf("%lld",&n1); a=(long long int *)malloc(sizeof(long long int)*n1); for(long long int i=0; i<n1; i++){ scanf("%lld",&a[i]); } scanf("%lld",&n2); b=(long long int *)malloc(sizeof(long long int)*n2); for(long long int i=0; i<n2; i++){ scanf("%lld",&b[i]); } long long int total=n1+n2; long long int res=0; if(total&0x1) res=findkth(a,n1,b,n2,total/2+1); else res=findkth(a,n1,b,n2,total/2); printf("%lld\n",res); return 0; }
相关文章推荐
- redmine 安装实现全过程细节
- DPDK代码内存分析
- 你知道别人怎么看你恢复它?
- 【转】高并发情况下的单例模式
- hdoj继续畅通工程
- JQuery框架之两对小括号()()的理解
- js实现将选中值累加到文本框的方法
- hadoop学习笔记 Hadoop进程
- Quartz异常的一些处理
- hdu 1671 Phone List(字典树·粉刷式标记)
- 登录时 按Enter 进入登录界面 或者下一行
- awk命令简单介绍
- pdf怎么修改文字
- 寒風的Cocos2dx之旅之地图编辑器的使用
- 物理Standby Switchover 角色转换
- python---内建函数学习-2
- freemarker语法
- camel发布restlet
- time命令中的real,user,sys
- C语言中extern的用法