Pat(Advanced Level)Practice--1029(Median)
2014-03-09 09:38
417 查看
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 Input
4 11 12 13 14 5 9 10 15 16 17
Sample Output
13
最容易想到的方法:
#include<cstdio> #include<vector> #include<algorithm> #define MAX 2000005 using 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]); else printf("%lld\n",v[(m+n)/2]); return 0; }
然后可想而知,有两个case超时。。。
然后想了想,模拟归并排序就AC了。
AC代码:
#include<cstdio> #define MAX 1000005 using 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 1029. Median (25)
- 【PAT】【Advanced Level】1029. Median (25)
- 1029. Median (25)【排序】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1029. Median (25)
- 【C++】PAT(advanced level)1029. Median (25)
- PAT (Advanced Level)1029. Median (25)
- PAT (Advanced Level) 1029. Median (25) 求两个有序数组的中位数,二分
- PAT (Advanced Level) 1029. Median (25)
- PAT (Advanced Level) Practise 1029 Median (25)
- PAT (Advanced Level) Practise 1029 Median (25)
- 1029. Median (25) @ PAT (Advanced Level) Practise
- 【PAT Advanced Level】1029. Median (25)
- Pat(Basic Level)Practice--1029(旧键盘)
- Pat(Advanced Level)Practice--1084(Broken Keyboard)
- Pat(Advanced Level)Practice--1010(Radix)
- Pat(Advanced Level)Practice--1066(Root of AVL Tree)
- Pat(Advanced Level)Practice--1098( Insertion or Heap Sort)
- Pat(Advanced Level)Practice--1050(String Subtraction)
- Pat(Advanced Level)Practice--1048(Find Coins)
- Pat(Advanced Level)Practice--1056(Mice and Rice)