1029. Median (25)

分析:

  考察归并排序,用简单的快排会超时。

 1 #include <iostream>

 2 #include <stdio.h>

 3 #include <algorithm>

 4 #include <cstring>

 5 #include <string>

 6 #include <vector>

 7 #include <cctype>

 8 #include <stack>

 9 #include <map>

10 

11 using namespace std;

12 

13 //    归并排序

14 

15 int a[1000005];

16 int b[1000005];

17 int c[2000005];

18 

19 int merge(int n, int m)

20 {

21     int i = 0, j = 0, k = 0;

22     while (i < n && j < m)

23     {

24         if (a[i] > b[j])

25             c[k++] = b[j], j++;

26         else

27             c[k++] = a[i], i++;

28     }

29     while (i < n)

30         c[k++] = a[i], i++;

31     while (j < m)

32         c[k++] = b[j], j++;

33     return k;

34 }

35 

36 int main()

37 {

38     int n, m;

39     while (scanf("%d", &n) != EOF)

40     {

41         for (int i = 0; i < n; i++)

42             scanf("%d", &a[i]);

43 

44         scanf("%d", &m);

45         for (int i = 0; i < m; i++)

46             scanf("%d", &b[i]);

47 

48         int l = merge(n, m);

49 

50         if (l % 2 == 1)

51             printf("%d\n", c[l >> 1]);

52         else

53             printf("%d\n", c[(l - 1) >> 1]);

54     }

55     return 0;

56 }
View Code

 

你可能感兴趣的:(media)