标签:
Description
Input
Output
Sample Input
Sample Output
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int HEAD = 0; 8 const int SIZE = 1005 * 1005; 9 10 int N,ANS; 11 int U[SIZE],D[SIZE],L[SIZE],R[SIZE],C[1005]; 12 13 void ini(void); 14 void dancing(int); 15 void remove(int); 16 void resume(int); 17 int cut(void); 18 void debug(int); 19 int main(void) 20 { 21 int t; 22 long long s[1005]; 23 24 //freopen("txt.txt","r",stdin); 25 scanf("%d",&t); 26 while(t --) 27 { 28 scanf("%d",&N); 29 for(int i = 1;i <= N;i ++) 30 scanf("%lld",&s[i]); 31 32 ini(); 33 int count = N + 1; 34 for(int i = 1;i <= N;i ++) 35 { 36 int first = count; 37 for(int j = 1;j <= N;j ++) 38 if(s[i] % s[j] == 0 || s[j] % s[i] == 0) 39 { 40 U[count] = U[j]; 41 D[count] = j; 42 L[count] = count - 1; 43 R[count] = count + 1; 44 45 D[U[count]] = count; 46 U[j] = count; 47 C[count] = j; 48 49 count ++; 50 } 51 L[first] = count - 1; 52 R[count - 1] = first; 53 } 54 dancing(0); 55 printf("%d\n",ANS); 56 } 57 58 return 0; 59 } 60 61 62 void ini(void) 63 { 64 ANS = 0; 65 R[HEAD] = 1; 66 L[HEAD] = N; 67 for(int i = 1;i <= N;i ++) 68 { 69 L[i] = i - 1; 70 R[i] = i + 1; 71 U[i] = D[i] = C[i] = i; 72 } 73 R[N] = 0; 74 } 75 76 void dancing(int k) 77 { 78 if(k + cut() <= ANS) 79 return ; 80 if(R[HEAD] == HEAD) 81 { 82 ANS = ANS > k ? ANS : k; 83 return ; 84 } 85 86 int c = R[HEAD]; 87 88 for(int i = D[c];i != c;i = D[i]) 89 { 90 remove(i); 91 for(int j = R[i];j != i;j = R[j]) 92 remove(j); 93 dancing(k + 1); 94 for(int j = L[i];j != i;j = L[j]) 95 resume(j); 96 resume(i); 97 } 98 99 return ; 100 } 101 102 void remove(int c) 103 { 104 for(int i = D[c];i != c;i = D[i]) 105 { 106 L[R[i]] = L[i]; 107 R[L[i]] = R[i]; 108 } 109 } 110 111 void resume(int c) 112 { 113 for(int i = U[c];i != c;i = U[i]) 114 { 115 L[R[i]] = i; 116 R[L[i]] = i; 117 } 118 } 119 120 void debug(int count) 121 { 122 for(int i = 0;i <= count;i ++) 123 printf("U[%d]=%d D[%d]=%d L[%d]=%d R[%d]=%d c[%d]=%d\n",i,U[i],i,D[i],i,L[i],i,R[i],i,C[i]); 124 return ; 125 } 126 127 int cut(void) 128 { 129 int sum = 0; 130 for(int i = R[HEAD];i;i = R[i]) 131 sum ++; 132 return sum; 133 }
标签:
原文地址:http://www.cnblogs.com/xz816111/p/4424825.html