HDU 1455 Sticks DFS is also a pattern pruning, tired sleep do not love

Source: Internet
Author: User

SticksTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6750 Accepted Submission (s): 1967


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

This problem tle n times, also wa a lot of times, is a long lesson paste two code bar don't ask me why, have code humanity!
Code:
#include <cstdio> #include <algorithm> #include <cstring>using namespace std; int n, d[70], T;bool visit ED[70], Notprime[65*55];bool cmp (const int A, const int b) {return a>b;} BOOL Dfs (int len, int sum, int c, int pos) {if (c = = t) {return true;} else if (len = = sum) {if (Dfs (len,0,c+1,0)) {return true;}} else{for (int i = pos; i < n; ++i) {if (!visited[i]) {if (Sum+d[i]>len) {continue;} Visited[i] = true; if (Dfs (len,sum+d[i],c,i+1)) {return true;} Visited[i] = false;//This step pruning is important, it is not possible to time out if (sum = = 0) {return false;} If the next length is equal to the current, then there is no need to repeat the search while (d[i] = = d[i+1]) ++i;}} return false;}  int main () {while (~SCANF ("%d", &n) && n) {int sum = 0; for (int i = 0; i < n; ++i) {scanf ("%d", &d[i]); sum + = D[i];} BOOL flag = False, sort (d,d+n,cmp), for (int i = d[0], I <= sum/2; ++i) {if (sum%i = = 0)//pruning, when and only if sum divisible by I, it is possible {t = sum/ i; memset (visited,false,sizeof (visited)); if (Dfs (i,0,1,0)) {printf ("%d\n", i); flag = true; break;}}} if (!flag) {printf ("%d\n", Sum);}} RetUrn 0;} 

Code:
#include <cstdio> #include <algorithm> #include <cstring>using namespace std; int n, d[70]; BOOL visited [[], Notprime[65*55];bool CMP (const int A, const int b) {return a>b;} BOOL Dfs (int len, int sum, int c, int pos) {if (c = = N) {return true;} else if (len = = sum) {if (Dfs (len,0,c,0)) {return true;}} else{for (int i = pos; i < n; ++i) {if (!visited[i]) {if (Sum+d[i]>len) {continue;} Visited[i] = true; if (Dfs (len,sum+d[i],c+1,i+1)) {return true;} Visited[i] = false;//This step pruning is important, it is not possible to time out if (sum = = 0) {return false;} If the next length is equal to the current, then there is no need to repeat the search while (d[i] = = d[i+1]) ++i;}} return false;}  int main () {while (~SCANF ("%d", &n) && n) {int sum = 0; for (int i = 0; i < n; ++i) {scanf ("%d", &d[i]); sum + = D[i];} BOOL flag = False, sort (d,d+n,cmp), for (int i = d[0], I <= sum/2; ++i) {if (sum%i = = 0)//pruning, when and only if sum can divide I, {memset (v Isited,false,sizeof (visited)); if (Dfs (i,0,1,0)) {printf ("%d\n", i); flag = true; break;}}} if (!flag) {printf ("%d\n", Sum);}} return 0;}

with June

HDU 1455 Sticks DFS is also a pattern pruning, tired sleep do not love

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.