Pat(Advanced Level)Practice--1029(Median)
来源:互联网 发布:怎么用网络看电视直播 编辑:程序博客网 时间:2024/06/05 07:53
Pat1029 代码
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<vector>#include<algorithm>#define MAX 2000005using namespace std;long long v[MAX];int main(int argc,char *argv[]){long long n,m;long long i,j,temp;scanf("%lld",&n);for(i=0;i<n;i++){scanf("%lld",&temp);v[i]=temp;}scanf("%lld",&m);for(j=0;j<m;j++){scanf("%lld",&temp);v[i]=temp;i++;}sort(v,v+m+n);if((m+n)%2==0)printf("%lld\n",v[(m+n)/2-1]);elseprintf("%lld\n",v[(m+n)/2]);return 0;}
然后可想而知,有两个case超时。。。
#include<cstdio>#define MAX 1000005using namespace std;void Output(int N[],int M[],int ans,int n,int m,int count);int N[MAX],M[MAX];int main(int argc,char *argv[]){int n,m;int count,i,j;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&N[i]);scanf("%d",&m);for(i=0;i<m;i++)scanf("%d",&M[i]);i=j=0;count=0;int ans;while(i<n&&j<m){if(N[i]<M[j]){ans=N[i];count++;i++;}else if(N[i]==M[j]){ans=N[i];count++;i++;Output(N,M,ans,n,m,count);count++;j++;}else{ans=M[j];count++;j++;}Output(N,M,ans,n,m,count);}if(i==n){while(j<m){ans=M[j];count++;j++;Output(N,M,ans,n,m,count);}}else if(j==m){while(i<n){ans=N[i];count++;i++;Output(N,M,ans,n,m,count);}}return 0;}void Output(int N[],int M[],int ans,int n,int m,int count){if((m+n)%2==0){if(count==(m+n)/2){printf("%d\n",ans);return;}}else{if(count==(m+n)/2+1){printf("%d\n",ans);return;}}}
中间有试过其它方法,但都有部分case超时。。。
- Pat(Advanced Level)Practice--1029(Median)
- PAT (Advanced Level) Practise 1029 Median (25)
- PAT (Advanced Level) Practise 1029 Median (25)
- PAT (Advanced Level) Practice 1006
- Pat(Advanced Level)Practice--1025(PAT Ranking)
- Pat(Advanced Level)Practice--1075(PAT Judge)
- Pat(Advanced Level)Practice--1003(Emergency)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- Pat(Advanced Level)Practice--1008(Elevator)
- Pat(Advanced Level)Practice--1010(Radix)
- Pat(Advanced Level)Practice--1015(Reversible Primes)
- Pat(Advanced Level)Practice--1024(Palindromic Number)
- Pat(Advanced Level)Practice--1028(List Sorting)
- Pat(Advanced Level)Practice--1030(Travel Plan)
- Pat(Advanced Level)Practice--1035(Password)
- Pat(Advanced Level)Practice--1037(Magic Coupon)
- Pat(Advanced Level)Practice--1032(Sharing)
- Pat(Advanced Level)Practice--1041(Be Unique)
- 面试回顾
- C++编程中是不是线程越多越好?
- 这里主要针对Mapreduce的性能调优。
- YT新人之巅峰大决战04
- UITableView详解(UITableViewCell(一)重中之重)
- Pat(Advanced Level)Practice--1029(Median)
- typedef用法小结
- 别做HR最讨厌的求职者
- win32程序启动的过程和启动速度的优化
- poj.1789
- SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item
- JavaEE的13种核心技术
- poj2387
- C++类中的变量和构造函数