【HDU】3335 Divisibility

 1 #include<cstdio>

 2 #include<algorithm>

 3 #define MAXM 1010

 4 #define MAXN 1000000

 5 #define INF 0x7FFFFFFF

 6 typedef long long LL;

 7 using namespace std;

 8 LL a[MAXM];

 9 int size, ans;

10 int L[MAXN], R[MAXN], U[MAXN], D[MAXN];

11 int H[MAXN], C[MAXN], S[MAXN];

12 void Init(int n) {

13     int i;

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

15         R[i] = i + 1;

16         L[i + 1] = i;

17         U[i] = D[i] = i;

18         S[i] = 0;

19         H[i] = -1;

20     }

21     R[n] = 0;

22     size = n + 1;

23 }

24 void Link(int r, int c) {

25     D[size] = D[c];

26     U[size] = c;

27     U[D[c]] = size;

28     D[c] = size;

29     if (H[r] < 0)

30         H[r] = L[size] = R[size] = size;

31     else {

32         L[size] = H[r];

33         R[size] = R[H[r]];

34         L[R[H[r]]] = size;

35         R[H[r]] = size;

36     }

37     S[c]++;

38     C[size++] = c;

39 }

40 int A() {

41     int i, res;

42     for (res = 0, i = R[0]; i; i = R[i])

43         res++;

44     return res;

45 }

46 void Remove(int c) {

47     int i;

48     for (i = D[c]; i != c; i = D[i]) {

49         L[R[i]] = L[i];

50         R[L[i]] = R[i];

51     }

52 }

53 void Resume(int c) {

54     int i;

55     for (i = D[c]; i != c; i = D[i])

56         L[R[i]] = R[L[i]] = i;

57 }

58 void Dance(int now) {

59     if (R[0] == 0)

60         ans = max(ans, now);

61     else if (now + A() > ans) {

62         int i, j, temp, c;

63         for (temp = INF,i = R[0]; i; i = R[i]) {

64             if (temp > S[i]) {

65                 temp = S[i];

66                 c = i;

67             }

68         }

69         for (i = D[c]; i != c; i = D[i]) {

70             Remove(i);

71             for (j = R[i]; j != i; j = R[j])

72                 Remove(j);

73             Dance(now + 1);

74             for (j = L[i]; j != i; j = L[j])

75                 Resume(j);

76             Resume(j);

77         }

78     }

79 }

80 int main() {

81     int c, i, j, n;

82     scanf("%d", &c);

83     while (c--) {

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

85         Init(n);

86         for (i = 1; i <= n; i++)

87             scanf("%I64d", &a[i]);

88         for (i = 1; i <= n; i++) {

89             for (j = 1; j <= n; j++) {

90                 if (a[i] % a[j] == 0 || a[j] % a[i] == 0)

91                     Link(i, j);

92             }

93         }

94         ans = 0;

95         Dance(0);

96         printf("%d\n", ans);

97     }

98     return 0;

99 }

你可能感兴趣的:(visibility)