HDOJ-1052 Tian Ji -- The Horse Racing

:根据题目的描述,问题可以转化为“找到最大权值的完美二分匹配”。但又说:杀鸡焉用宰牛刀。暗示了有更简单的方法。

:个人认为用到了贪心的思想“用最差的马搏最好的马”。

View Code
 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <stdlib.h>

 4 #define NL 1100

 5 

 6 int tian[NL], king[NL];

 7 

 8 int cmp(const void *a, const void *b)

 9 {

10     return *(int*)a - *(int*)b;

11 }

12 

13 int main()

14 {

15     int n;

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

17         int i, j;

18         for (i=0; i<n; i++) {

19             scanf("%d", &tian[i]);

20         }

21         for (i=0; i<n; i++) {

22             scanf("%d", &king[i]);

23         }

24         qsort(tian, n, sizeof(tian[i]), cmp);

25         qsort(king, n, sizeof(king[i]), cmp);

26 /*        for (i=0; i<n; i++) {

27             printf("%d\n", king[i]);

28         }            */

29         int t1, t2, k1, k2, win=0;

30         t1 = k1 =  0;

31         t2 = k2 = n-1;

32         while (t1 <= t2 && k1 <= k2) {

33             if (tian[t1] > king[k1]) {

34                 ++t1;

35                 ++k1;

36                 ++win;

37             }else if (tian[t2] > king[k2]) {

38                 --t2;

39                 --k2;

40                 ++win;

41             }else if (tian[t1] < king[k1]) {

42                 ++t1;

43                 --k2;

44                 --win;

45             }else if (tian[t2] < king[k2]) {

46                 ++t1;

47                 --k2;

48                 --win;

49             }else if (tian[t1] < king[k2]) {

50                 ++t1;

51                 --k2;

52                 --win;

53             }else if (tian[t1] > king[k2]){

54                 ++t1;

55                 --k2;

56                 ++win;                

57             }else {

58                 ++t1;

59                 --k2;

60             }

61         }

62 //        printf("t = %d\n", t);

63         printf("%d\n", 200*win);

64     }

65     return 0;

66 }

67 /*

68 4

69 1 2 3 4

70 2 3 4 1

71 3

72 1 1 1

73 1 1 1

74 3

75 90 80 70

76 90 89 79

77 */

 

你可能感兴趣的:(RAC)