4.1.2 Fence Rails 栅栏的木料
时间: 1ms 内存:64M
描述:
农民John准备建一个栅栏来围住他的牧场。他已经确定了栅栏的形状,但是他在木料方面有些问题。当地的杂货储存商扔给John一些木板,而John必须从这些木板中找出尽可能多所需的木料。
当然,John可以切木板。因此,一个9英尺的木板可以切成一个5英尺和一个4英尺的木料 (当然也能切成3个3英尺的,等等)。John有一把梦幻之锯,因此他在切木料时,不会有木料的损失。
所需要的木料规格都已经给定。你不必切出更多木料,那没有用。
输入:
第1行: N (1 <= N <= 50), 表示提供的木板的数目 第2行到第N+1行: N行,每行包括一个整数,表示各个木板的长度。 第N+2行: R (1 <= R <= 1023), 所需木料的数目 第N+3行到第N+R+1行: R行,每行包括一个整数(1 <= ri <= 128)表示所需木料的长度。
输出:
只有一行,一个数字,表示能切除的最多的所需木料的树木。当然,并不是任何时候都能切出所有所需木料。
示例输入:
4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30
示例输出:
7
提示:
参考答案(内存最优[812]):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n, m;
int t[55];
int temp[55];
int leng[1025];
int sum[1025];
int need[1025];
int mid;
int cmp2(const void *a, const void *b)
{
return (*(int*)a)-(*(int*)b);
}
int cmp1(const void *a, const void *b)
{
return (*(int*)b)-(*(int*)a);
}
int dfsid( int cnt, int i, int tempsum)
{
if (!cnt)
return 1;
if (tempsum > sum[n] - need[mid])
return 0;
int j = (leng[cnt] == leng[cnt - 1] ? i : 1);
for (j ; j <= n; j++)
{
if (leng[cnt] <= temp[j])
{
temp[j] -= leng[cnt];
if (temp[j] < leng[1])
tempsum += temp[j];
if (dfsid(cnt-1, j, tempsum))
return 1;
if (temp[j] < leng[1])
tempsum -= temp[j];
temp[j] += leng[cnt];
}
}
return 0;
}
int solve()
{
int i;
qsort(&t[1], n, sizeof(int), cmp1);
qsort(&leng[1], m, sizeof(int), cmp2);
while (t[n] < leng[1] && n)
n--;
while (leng[m] > t[1] && m)
m--;
for (i=1; i<=n; i++)
sum[i] = sum[i-1] + t[i];
for (i=1; i<=m; i++)
need[i] = need[i-1] + leng[i];
int l = 0;
int r = m + 1;
while (l < r - 1)
{
for (i=1; i<=n; i++)
temp[i] = t[i];
mid = (l + r) / 2;
if (dfsid(mid, 1, 0))
l = mid;
else
r = mid;
}
printf("%d\n", l);
return 0;
}
int main()
{
int i;
scanf("%d", &n);
for (i=1; i<=n; i++)
scanf ("%d", &t[i]);
scanf ("%d", &m);
for (i=1; i<=m; i++)
scanf ("%d", &leng[i]);
solve();
return 0;
}
参考答案(时间最优[68]):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int n, m;
int t[55];
int temp[55];
int leng[1025];
int sum[1025];
int need[1025];
int mid;
int cmp2(const void *a, const void *b)
{
return (*(int*)a)-(*(int*)b);
}
int cmp1(const void *a, const void *b)
{
return (*(int*)b)-(*(int*)a);
}
int dfsid( int cnt, int i, int tempsum)
{
if (!cnt)
return 1;
if (tempsum > sum[n] - need[mid])
return 0;
int j = (leng[cnt] == leng[cnt - 1] ? i : 1);
for (j ; j <= n; j++)
{
if (leng[cnt] <= temp[j])
{
temp[j] -= leng[cnt];
if (temp[j] < leng[1])
tempsum += temp[j];
if (dfsid(cnt-1, j, tempsum))
return 1;
if (temp[j] < leng[1])
tempsum -= temp[j];
temp[j] += leng[cnt];
}
}
return 0;
}
int solve()
{
int i;
qsort(&t[1], n, sizeof(int), cmp1);
qsort(&leng[1], m, sizeof(int), cmp2);
while (t[n] < leng[1] && n)
n--;
while (leng[m] > t[1] && m)
m--;
for (i=1; i<=n; i++)
sum[i] = sum[i-1] + t[i];
for (i=1; i<=m; i++)
need[i] = need[i-1] + leng[i];
int l = 0;
int r = m + 1;
while (l < r - 1)
{
for (i=1; i<=n; i++)
temp[i] = t[i];
mid = (l + r) / 2;
if (dfsid(mid, 1, 0))
l = mid;
else
r = mid;
}
printf("%d\n", l);
return 0;
}
int main()
{
int i;
scanf("%d", &n);
for (i=1; i<=n; i++)
scanf ("%d", &t[i]);
scanf ("%d", &m);
for (i=1; i<=m; i++)
scanf ("%d", &leng[i]);
solve();
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。