Original title: Https://uva.onlinejudge.org/external/13/1354.pdf
There is a stone in s, each piece is hung by a rope, if there are two or more stones, the balance of the need to put all the stones hanging up.
The width of the room is R, asking for the maximum width of the balance that is smaller than the room width R.
Analysis : is a backtracking enumeration problem, enumeration midway if the current width is found to be greater than R, backtracking.
Difficulty : Also can be said is the bright spot, is enumerates all two forks the tree, a balance can be regarded as a binary tree.
Specifically, the process of building a two-fork tree recursively is to select two nodes each time from a collection that contains all the nodes, one
So our process is to determine the leaf node first, and then merge the generation parent node.
Because the two-fork tree we built is characterized by the fact that all the leaf nodes are the stones we want to hang, all the parent nodes are a balance
Details : This problem has a special pit details, card me WA long time, is two fork tree the right node may overlap with the left node
And it is possible that the left lever length of the right node is greater than the left side's lever length, and vice versa.
Therefore, this special case needs to be considered when generating the parent node to calculate the total length of the left and right balances.
#include <cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN = A;intN, IDX, F[MAXN], W[MAXN];DoubleR, ans, RW[MAXN], LW[MAXN];voidBuild_binary_tree (intDEP) { if(dep = = N)return; for(inti =0; i < MAXN; i++)if(F[i]) for(intj =0; J < Maxn; J + +)if(I! = J &&F[j]) { DoubleL = Max (Lw[i], Lw[j]-1), R = Max (Rw[i]-1, Rw[j]); if(1+ L + R <r) {if(dep = = N-1) ans = max (1+ L +R, ans); F[i]= F[j] =0; intid = idx++; F[id]=1; W[id]= W[i] +W[j]; Lw[id]= W[j] *1.0/W[id] +L; Rw[id]= W[i] *1.0/W[id] +R; Build_binary_tree (DEP+1); f[--IDX] =0; F[i]= F[j] =1; } }}intMain () {intT; scanf ("%d", &T); while(t--) {scanf ("%lf%d", &r, &N); Ans= -1; IDX =0; Memset (F,0,sizeof(f)); memset (rw,0.0,sizeof(rw)); memset (LW,0.0,sizeof(LW)); for(inti =0; I < n; i++) {scanf ("%d", &W[i]); F[idx++] =1; } if(n = =1) {printf ("0.0000000000\n");Continue;} Build_binary_tree (1); printf ("%.10lf\n", ans); } return 0;}
Mobile computing-Balance Puzzle-uva1354 (Backtracking enumeration binary tree)