Sticks
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 110743 | Accepted: 25413 |
Description
George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.
Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 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
9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
Sample Output
6 5
Source
木棒
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 110744 | Accepted: 25413 |
Description
乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位。然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度。请你设计一个程序,帮助乔治计算木棒的可能最小长度。每一节木棍的长度都用大于零的整数表示。
Input
输入包含多组数据,每组数据包括两行。第一行是一个不超过64的整数,表示砍断之后共有多少节木棍。第二行是截断以后,所得到的各节木棍的长度。在最后一组数据之后,是一个零。
Output
为每组数据,分别输出原始木棒的可能最小长度,每组数据占一行。
Sample Input
9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
Sample Output
6 5
Source
Translator
北京大学程序设计实习, Xie Di
果然搜索还不到家,这道题,基本参考着别人的剪枝写的,然后还写的不伦不类总是过不去,结果最后基本就是抄别人的写出来的嘛...(>_<),唉,以后再感受一次吧.......实在是精疲力尽了,果然脑子不够用啊......
#include<iostream>
#include<cmath>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#define MAX_LEN 111
int dfs(int vis[MAX_LEN], int stick[MAX_LEN], int objlen, int nowlen, int nowstick, int numstick, int int_64);
void my_qsort(int l, int r, int stick[MAX_LEN]);
int main(void)
{
int stick[MAX_LEN];
int vis[MAX_LEN];
int int_64;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// printf("hello world");
while(scanf("%d", &int_64) && int_64)
{
int maxint;
int sumint = 0;
for (int i = 0;i < int_64;i ++)
{
scanf("%d", &stick[i]);
sumint += stick[i];
vis[i] = 0;
}
my_qsort(0, int_64, stick);
// for (int i = 0;i < int_64;i ++)
// printf("%d ", stick[i]);
// printf("\n");
maxint = stick[0];
int have = 0;
for (int i = maxint;i <= sumint - i;i ++)
{
if (!(sumint % i) && dfs(vis, stick, i, 0,0,0, int_64))
{
printf("%d\n", i);
have = 1;
break;
}
}
if(!have)
{
printf("%d\n", sumint);
}
}
}
int dfs(int vis[MAX_LEN], int stick[MAX_LEN], int objlen, int nowlen, int nowstick, int numstick, int int_64)
{
if (numstick == int_64)
return 1;
int repeat = -1;
for (int i = nowstick ;i < int_64;i ++)
{
if (vis[i] || stick[i] == repeat)
continue;
vis[i] = 1;
if (nowlen + stick[i] < objlen)
{
if (dfs(vis, stick, objlen, nowlen+stick[i], i, numstick+1, int_64))
return 1;
else repeat = stick[i];
}
else if (nowlen + stick[i] == objlen)
{
if (dfs(vis, stick ,objlen, 0, 0, numstick+1, int_64))
return 1;
else repeat = stick[i];
}
vis[i] = 0;
if(nowlen == 0)
break;
}
return 0;
}
void my_qsort(int l, int r, int stick[MAX_LEN])
{
if (r < l + 2)
return;
int ll = l, rr = r;
while (l < r)
{
while(++l < rr && stick[l] > stick[ll]);
while(--r > ll && stick[r] < stick[ll]);
if (l < r)
{
int temp = stick[l];
stick[l] = stick[r];
stick[r] = temp;
}
}
int temp = stick[r];
stick[r] = stick[ll];
stick[ll] = temp;
my_qsort(ll, r, stick);my_qsort(l, rr, stick);
}