POJ1011 sticks "pruning"

Source: Internet
Author: User

Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 122771 Accepted: 28441

Description

George took sticks of the same length and cut them randomly until all parts became at most units long. Now he wants to return sticks to the original state, but he forgot what many sticks he had originally and how long they wer E 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.

Input

The 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.

Output

The output should 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

Source

Central Europe 1995
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace Std;int len[66], Tarlen, tarcnt, N, Sum, Cnt;bool vis[66];bool DFS (int k, int leftlen) {int i;if (Leftlen = 0) {if (++cnt = = tarcnt) return true;for (i = 0; i < N; ++i) if (!vis[i]) {Vis[i] = 1;if (DFS (i + 1, tarlen-len[i]) return True;else {VI S[i] = 0;--cnt;return false;}}} for (i = k; i < N; ++i) {if (!vis[i] && len[i] <= leftlen) {if (len[i] = = Len[i-1] &&!vis[i-1]) continu E;vis[i] = 1;if (DFS (i + 1, leftlen-len[i])) return true;vis[i] = 0;}} return false;} int main () {int I, J;while (scanf ("%d", &n), N) {for (i = sum = 0; i < N; ++i) {scanf ("%d", &len[i]); sum + = Len[i ];}  Sort (len, Len + N, greater<int> ()); for (Tarlen = len[0]; tarlen < sum; ++tarlen) {if (sum% tarlen) continue;tarcnt = Sum/tarlen;memset (Vis, 0, sizeof (VIS)); vis[0] = 1; CNT = 0;if (DFS (1, tarlen-len[0])) break; printf ("%d\n", Tarlen);} return 0;}



POJ1011 sticks "pruning"

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.