搜索题。

枚举最小长度进行搜索。但是考虑到先进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代码

相关文章:

  • 2021-12-09
  • 2021-07-27
  • 2021-12-24
  • 2022-12-23
  • 2021-10-11
  • 2021-10-01
猜你喜欢
  • 2021-08-20
  • 2021-11-30
  • 2022-01-29
相关资源
相似解决方案