SticksTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8682 Accepted Submission (s): 2561
Problem Descriptiongeorge took sticks of the same length and cut them randomly until all parts became at most units lon G. Now he wants to return sticks to the original state, but he forgot what many sticks he had originally and how long they were originally. Him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units is integers greater than zero.
Inputthe input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there is at most sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
Outputthe output file contains the smallest possible length of original sticks, one per line.
Sample Input
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65
Test instructions: The number of n is divided into M-heap, and each heap is equal to H, and the minimum is the number of H.
There are some numbers in the title:
50: The length of each piece of rope after the cut is 50 maximum
64: The maximum number of ropes after shearing is 64
And note that there is a possibility that there is no reduction in the rope at all
Pruning:
(1) Stop searching when the remaining length is equal to the current length
(2) The remaining length is equal to the total length of the rope, the current length does not match, stop searching
(3) The current length does not meet the criteria, then the same length is no longer searched
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm>using namespace Std;int flag,n,maxn;int used[100],a[100];int cmp (int x,int y) {return x>y;} void Dfs (int ans,int num_k,int dd) {if (flag==1) return; if (num_k==n) {flag=1; return; } if (ans==0 && num_k<n) DFS (maxn,num_k,0); for (int i=dd;i<n;i++) {if (!flag &&!used[i] && a[i]<=ans) {used[i]=1; DFS (ANS-A[I],NUM_K+1,DD); used[i]=0; if (Ans==a[i]) return; if (ANS==MAXN) return; while (a[i]==a[i+1]) ++i; }}}int Main () {while (~SCANF ("%d", &n)) {if (n==0) break; int sum=0; for (int i=0;i<n;i++) {scanf ("%d", &a[i]); Sum+=a[i]; } sort (a,a+n,cmp); MAXN=A[0]; flag=0; while (!flag) {while (sum%maxn!=0) ++MAXN; memset (used,0,sIzeof (used)); DFS (maxn,0,0); if (flag) break; ++MAXN; } printf ("%d\n", MAXN); } return 0;}
Mob Search + Backtracking--sticks