poj1011 | Nkoj 1423 Sticks

Source: Internet
Author: User
Tags sort time limit
P1423 "Classification Exercise 6. Search" Small sticks
Time limit: 10000 MS space limit: 65536 KB
Problem Description

George had some of the same long little sticks, and he cut the sticks into pieces at random, until the length of each paragraph was no more than 50.

Now, he wants to splice the sticks into the original, but forgets how many sticks and lengths they had at the beginning.

Given the length of each small stick, programming helps him figure out the minimum possible length of the original stick. Input Format

There are two lines in the input file.

The first act of a single integer n indicates the total number of small wooden cabinets that have been seen in the future, of which n≤60,

The second behavior n a positive integer separated by an empty one, indicating the length of n and the small stick. output Format

The output file is only one line, indicating the minimum possible length of the original stick required. Sample Input


5 2 1 5 2 1 5 2 1 Sample output: 6
Analysis: The first answer must be the sum of the lengths of all sticks, and longer than the longest stick. Enumerate each of these numbers from small to large, using DFS to authenticate. DFS idea: Assemble a long piece of wood and fill up the quantity. DFS (x, used, cur) represents the current trial of the X-sticks, the current large wooden bar has been used used meters, the total length of the large wooden bar. Pruning: (1) Each end of a large wooden bar, the new large wooden bar with the remaining sticks in the longest one, so that the back of the binding ability stronger. (2) (strong) If you have the same stick, discuss it only once. It seems that these two are already able to be a. sort all sticks from large to small for ease of handling. The code is as follows:

#include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <cstring > #include <queue> #include <vector> #include <algorithm> #define LL Long long #define CLEAR (XXX)
memset (xxx,0,sizeof (XXX)) using namespace Std;

const int MAXN=70,INF=1E9;
int N,len,s[maxn],tot,_min,_max;
Vector<int> divs;

BOOL VIS[MAXN];
	void depart (int p) {//Find the Factor divs.clear () to enumerate;
	int m= (int) sqrt (p);
			for (int i=1;i<=m;i++) if (p%i==0) {if (I>=_max) divs.push_back (i);
		if (I*i!=p&&p/i>=_max) divs.push_back (p/i);
} sort (Divs.begin (), Divs.end ());
	} bool Dfs (int x,int used,int cur) {int i;
		if (len==used) {Cur+=len;
		if (Cur==tot) return true;
		X=1;  while (Vis[x]) x + +;
		Because it is out of order, find the longest remaining stick vis[x]=true;
		if (Dfs (x,s[x],cur)) return true;
	Vis[x]=false; } else {for (i=x;i<=n;i++) {if (i>1&&s[i]==s[i-1]&&!vis[i-1]) continue;//Avoid repeated searches for the same length if (!vi S[i]&&used+s[i]<=len) {Vis[i]=true;
				if (Dfs (i,used+s[i],cur)) return true;
			Vis[i]=false;		
}}} return false;

} BOOL cmp (int x,int y) {return x>y;}
    int main () {int i,j;
    	while (cin>>n&&n) {tot=_max=0; _min=inf;
			for (i=1;i<=n;i++) {cin>>s[i];
			Tot+=s[i];
		_max=max (_max,s[i]);
		} sort (s+1,s+1+n,cmp);
		Depart (TOT); 
			For (I=0;i<divs.size (); i++) {CLEAR (VIS);
			Len=divs[i];
				if (Dfs (1,0,0)) {cout<<divs[i]<<endl;
			Break
}}} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.