Codeforces Round #129 (Div. 2) D. Little Elephant and Cards

题目链接

以前做过的题,比赛的时候没过,前后都一样的时候要特殊考虑一下,没什么印象了,就记得比赛完,又做的时候用qsort超时了,不会用sort,今天补上吧。代码写的很渣。。。

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <algorithm>

 5 using namespace std;

 6 #define N 10000000

 7 struct nn

 8 {

 9     int d;

10     int a;

11 } p[200001];

12 int cmp(const nn &a, const nn &b)

13 {

14     if(a.d < b.d )

15     return 1;

16     else

17     return 0;

18 }

19 int main()

20 {

21     int n,i,j,min,k,nn,su,z = 0;

22     int sv,ev;

23     scanf("%d",&n);

24     j = 0;

25     for(i = 0; i <= n-1; i ++)

26     {

27         scanf("%d%d",&sv,&ev);

28         if(sv != ev)

29         {

30             p[j].d = sv;

31             p[j].a = 1;

32             j ++;

33             p[j].d = ev;

34             p[j].a = 2;

35             j ++;

36         }

37         else

38         {

39             p[j].d = sv;

40             p[j].a = 1;

41             j ++;

42         }

43     }

44     su = j;

45     if(n%2 == 0)

46         nn = n/2;

47     else

48         nn = n/2+1;

49     sort(p,p+su,cmp);

50     j = 1;

51     min = N;

52     if(p[0].a == 1)

53         k = 1;

54     else

55         k = 0;

56     for(i = 1; i <= su-1; i ++)

57     {

58         if(p[i].d == p[i-1].d)

59         {

60             j ++;

61             if(p[i].a == 1)k ++;

62             if(k >= nn)

63             {

64                 z = 1;

65                 break;

66             }

67         }

68         else

69         {

70             if(j >= nn)

71             {

72                 if(min > nn-k)

73                     min = nn-k;

74             }

75             j = 1;

76             if(p[i].a == 1)

77                 k = 1;

78             else

79                 k = 0;

80         }

81         if(j + su-1-i < nn)

82         break;

83     }

84     

你可能感兴趣的:(codeforces)