PAT 1029

题目 : Median

分值 : 25
难度 : 简单题
思路 : 数据加强过了,用双指针
坑点 : 双指针 找到第 (N+M+1)/2 小的数就好了
评语 : 一开始还想着 bitmap 后来发现 long int 还长的 

具体代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>
using namespace std;
vector <int> v ;
int main()
{
int N,M,temp,cnt= 0 , count = 0 ;
cin >> N;
for(int i = 0 ; i< N ; i++)
{
scanf("%d" , &temp) ;
v.push_back(temp) ;
}
cin >> M ;
for(int i = 0 ; i< M ; i++)
{
scanf("%d" , &temp) ;
while(v[cnt] < temp && cnt<N )
{
count ++ ;
if(count == (N+M+1)/2)
{
printf("%d" , v[cnt] ) ;
return 0 ;
}
cnt ++ ;
}
count ++ ;
if(count == (N+M+1)/2)
{
printf("%d" , temp ) ;
break ;
}
}
while(cnt < N )
{
count ++ ;
if(count == (N+M+1)/2)
{
printf("%d" , v[cnt] ) ;
return 0 ;
}
cnt ++ ;
}

}