PAT(甲级)1029
来源:互联网 发布:淘宝新店如何提升信誉 编辑:程序博客网 时间:2024/05/17 07:31
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 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 Input4 11 12 13 145 9 10 15 16 17Sample Output
13
版本一:
#include <cstdio>#include <cstring>#define SIZE 1000006long a[SIZE];long b[SIZE];long c[2*SIZE];void Merge(long c[],long a[],long &num1,long b[],long &num2){long i,j,k;i=j=k=0;while(i<num1&&j<num2){if(a[i]<b[j]){c[k++]=a[i++];}else c[k++] = b[j++];}while(i<num1) c[k++] = a[i++];while(j<num2) c[k++] =b[j++];}int main(){memset(a,0,sizeof(long)*SIZE);memset(b,0,sizeof(long)*SIZE);memset(c,0,sizeof(long)*2*SIZE);long i,NUM1,NUM2;scanf("%ld",&NUM1);for(i=0;i<NUM1;i++) scanf("%ld",&a[i]);scanf("%ld",&NUM2);for(i=0;i<NUM2;i++) scanf("%ld",&b[i]);Merge(c,a,NUM1,b,NUM2);if((NUM1+NUM2)&0x1) printf("%ld\n",c[(NUM1+NUM2)/2]);else printf("%ld\n",c[(NUM1+NUM2)/2-1]);return 0;}
版本二:
#include <cstdio>#include <cstdlib>#define SIZE 1000006long a[SIZE];long b[SIZE];//can ignore the auxiliaray array to minimize the storage//long c[2*SIZE];/*void Merge(long c[],long a[],long &num1,long b[],long &num2){long i,j,k;i=j=k=0;while(i<num1&&j<num2){if(a[i]<b[j]){c[k++]=a[i++];}else c[k++] = b[j++];}while(i<num1) c[k++] = a[i++];while(j<num2) c[k++] =b[j++];}*/int main(){//memset(a,0,sizeof(long)*SIZE);//memset(b,0,sizeof(long)*SIZE);long i,NUM1,NUM2,index;scanf("%ld",&NUM1);//long *a=(long *)calloc(NUM1,sizeof(long));for(i=0;i<NUM1;i++) scanf("%ld",&a[i]);scanf("%ld",&NUM2);//long *b=(long *)calloc(NUM2,sizeof(long));for(i=0;i<NUM2;i++) scanf("%ld",&b[i]);if((NUM1+NUM2)&0x1) //determine the location index=(NUM1+NUM2)/2+1;else index=(NUM1+NUM2)/2;long j,count=0;i=j=0;bool flag,found=false;while(i<NUM1&&j<NUM2){if(a[i]<b[j]){count++;if(count==index){flag=true;found=true;index=i;break;}i++;}else{count++;if(count==index){flag=false;found = true;index=j;break;}j++;}}if(found){if(flag) printf("%ld\n",a[index]);else printf("%ld\n",b[index]);}else if(i <NUM1){index = index-count+i-1;printf("%ld\n",a[index]);}else{index = index-count+j-1;printf("%ld\n",b[index]);} return 0;}
- PAT(甲级)1029
- 浙大PAT甲级 1029
- PAT甲级1029
- 浙大PAT甲级-1029
- PAT甲级 1029
- PAT 甲级
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- 百度offe面经及找工作一个月体会
- LeetCode----Longest Valid Parentheses
- PAT(甲级)1028
- Android下TextView实现跑马灯效果
- 微信大转盘游戏
- PAT(甲级)1029
- Android事件传递机制、手势检测
- Vagrant中搭建Docker虚拟环境(下)-Mac
- HDU 4169 Wealthy Family(树形背包)
- Activity--know where you are
- Android之手势的识别与处理(双击onDoubleTap、滑动onFling、拖动onScroll)
- 白话经典算法系列之十二 数组中只出现1次的两个数字(百度面试题)
- HTML(网页设计)必用的设计模式---------CSS网页编程
- iOS安全系列之 HTTPS (二)