Fight a lot written: planting trees

Subject description:

Links: https://www.nowcoder.com/questionTerminal/52f25c8a8d414f8f8fe46d4e62ef732c
Source: Cattle passenger network

is much smaller want to beautify your own manor. His estate next to a river, he hopes to plant a row of trees in the river, a total of M trees. N procured much smaller varieties of trees, the number of each species is Ai (the total number of tree happens to M). But he hoped that any two adjacent tree is not the same species. Little more than ask you to help design scheme of planting trees to meet the requirements.

 

Links: https://www.nowcoder.com/questionTerminal/52f25c8a8d414f8f8fe46d4e62ef732c
Source: Cattle-off network

Enter a description:
The first line contains a positive integer N, the number of species of trees. 
The second line contains a positive integer N, the i (1 <= i <= N) number represents the number of i-th species of trees.
Data range:
. 1 <= N <= 1000
. 1 <= M <= 2000


Output Description:
An output line, comprising positive integers M, respectively numbering i tree species (type number from 1 to N). If several possible solutions exist, lexicographically smallest program output. If the program that satisfies the conditions, output "-."

Example 1

Input:

3
4 2 1

Export

1 2 1 2 1 3 1

 

Analysis of ideas:

This problem needs to be solved with dfs and back. Also need pruning to prevent timeouts.

 

Code:

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<stdlib.h>
 4 #include<vector>
 5 #include<algorithm>
 6 #include<limits.h>
 7 using namespace std;
 8 
 9 int find_max(int tree[], int n)
10 {
11     int max_val = 0;
12     int max_idx = -1;
13     for(int i=0; i<n; i++)
14     {
15         if(tree[i] > max_val)
16         {
17             max_val = tree[i];
18             max_idx = i;
19         }
20     }
21     return max_idx;
22 }
23 
24 bool dfs(int tree[], int res[], int n, int& m, int step)
25 {
26     int idx = find_max(tree, n);
27     if(idx == -1)
28         return true;
29     if(tree[idx]*2> m+1) // 剪枝
30         return false;
31     if(tree[idx]*2 == m+1)
32     {
33         tree[idx]--;
34         m--;
35         res[step] = idx+1;
36         if(dfs(tree, res, n, m, step+1))
37             return true;
38         tree[idx]++;
39         m++;
40     }
41     else
42     {
43         for(int i=0; i<n; i++)
44         {
45             if(tree[i]>0 && (step==0 || res[step-1]!=i+1))
46             {
47                 tree[i]--;
48                 m--;
49                 res[step] = i+1;
50                 if(dfs(tree, res, n, m, step+1))
51                     return true;
52                 tree[i]++;
53                 m++;
54             }
55         }
56     }
57     return false;
58 }
59 
60 int main()
61 {
62     int n;
63     scanf("%d",&n);
64     int tree[n];
65     int num_tree = 0;
66     for(int i=0; i<n; i++)
67     {
68         scanf("%d", &tree[i]);
69         num_tree += tree[i];
70     }
71     int res[num_tree];
72     int total = num_tree;
73     if(dfs(tree, res, n, num_tree, 0))
74     {
75         for(int i=0; i<total; i++)
76         {
77             if(i < total-1)
78                 printf("%d ", res[i]);
79             else
80                 printf("%d\n", res[i]);
81         }
82     }
83     else
84     {
85         printf("-\n");
86     }
87     return 0;
88 }

 

Guess you like

Origin www.cnblogs.com/LJ-LJ/p/11256967.html