poj1011 Sticks

搜索题。

枚举最小长度进行搜索。但是考虑到先进A/先进B是等效的,我们规定只能从i往i + 1进同一根木棒。

sort是最原始的剪枝手段。然后还有,等长木棒的等效性,0长度时任一木棒的等效性。

这样我们就0msAC了

搜索的时候我把i写成了k直接爆0

 1 #include <cstdio>
 2 #include <algorithm>
 3 using std::sort;
 4 const int N = 100;
 5 
 6 int n, a[N], tot, len, cnt;
 7 bool vis[N];
 8 
 9 inline bool cmp(const int &a, const int &b) {
10     return a > b;
11 }
12 
13 bool DFS(int k, int now, int l) {
14     //printf("DFS: %d %d %d \n", k, now, l);
15     if(now == cnt + 1) {
16         return 1;
17     }
18     int last = -1;
19     for(int i = k; i <= n; i++) {
20         if(vis[i] || a[i] == last) continue;
21         //printf("%d \n", i);
22         last = a[i];
23         if(a[i] + l < len) {
24             vis[i] = 1;
25             bool t = DFS(i + 1, now, l + a[i]);
26             vis[i] = 0;
27             if(t) return 1;
28             if(!l) {
29                 return 0;
30             }
31         }
32         else if(a[i] + l == len) {
33             vis[i] = 1;
34             bool t = DFS(1, now + 1, 0);
35             vis[i] = 0;
36             if(t) return 1;
37             if(!l) {
38                 return 0;
39             }
40         }
41     }
42     return 0;
43 }
44 
45 int main() {
46     while(scanf("%d", &n) && n) {
47         tot = 0;
48         for(int i = 1; i <= n; i++) {
49             scanf("%d", &a[i]);
50             tot += a[i];
51         }
52         sort(a + 1, a + n + 1, cmp);
53         /*for(int i = 1; i <= n; i++) {
54             printf("%d ", a[i]);
55         }
56         puts("");*/
57         for(int i = a[1]; i <= tot; i++) {
58             if(tot % i == 0) {
59                 len = i;
60                 cnt = tot / i;
61                 //printf("i = %d len = %d cnt = %d \n", i, len, cnt);
62                 if(DFS(1, 1, 0)) {
63                     printf("%d\n", i);
64                     break;
65                 }
66             }
67         }
68     }
69     return 0;
70 }
AC代码
 1 #include <cstdio>
 2 #include <algorithm>
 3 using std::sort;
 4 const int N = 100;
 5 
 6 int n, a[N], tot, len, cnt;
 7 bool vis[N];
 8 
 9 inline bool cmp(const int &a, const int &b) {
10     return a > b;
11 }
12 
13 bool DFS(int k, int now, int l) {
14     //printf("DFS: %d %d %d \n", k, now, l);
15     if(now == cnt + 1) {
16         return 1;
17     }
18     for(int i = k; i <= n; i++) {
19         if(vis[i]) continue;
20         //printf("%d \n", i);
21         if(a[i] + l < len) {
22             vis[i] = 1;
23             bool t = DFS(1, now, l + a[i]);
24             vis[i] = 0;
25             if(t) return 1;
26         }
27         else if(a[i] + l == len) {
28             vis[i] = 1;
29             bool t = DFS(1, now + 1, 0);
30             vis[i] = 0;
31             if(t) return 1;
32         }
33     }
34     return 0;
35 }
36 
37 int main() {
38     while(scanf("%d", &n) && n) {
39         tot = 0;
40         for(int i = 1; i <= n; i++) {
41             scanf("%d", &a[i]);
42             tot += a[i];
43         }
44         //sort(a + 1, a + n + 1, cmp);
45         /*for(int i = 1; i <= n; i++) {
46             printf("%d ", a[i]);
47         }
48         puts("");*/
49         for(int i = a[1]; i <= tot; i++) {
50             if(tot % i == 0) {
51                 len = i;
52                 cnt = tot / i;
53                 //printf("i = %d len = %d cnt = %d \n", i, len, cnt);
54                 if(DFS(1, 1, 0)) {
55                     printf("%d\n", i);
56                     break;
57                 }
58             }
59         }
60     }
61     return 0;
62 }
去掉所有剪枝,TLE

 

posted @ 2018-06-09 13:40  garage  阅读(104)  评论(0编辑  收藏  举报