Fence Rails
Burch, Kolstad, and Schrijvers
Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber store has dropped off boards of varying lengths; Farmer John must create as many of the rails he needs from the supplied boards.
Of course, Farmer John can cut the boards, so a 9 foot board can be cut into a 5 foot rail and a 4 foot rail (or three 3 foot rails, etc.). Farmer John has an `ideal saw', so ignore the `kerf' (distance lost during sawing); presume that perfect cuts can be made.
The lengths required for the rails might or might not include duplicates (e.g., a three foot rail and also another three foot rail might both be required). There is no need to manufacture more rails (or more of any kind of rail) than called for the list of required rails.
PROGRAM NAME: fence8
INPUT FORMAT
Line 1: | N (1 <= N <= 50), the number of boards |
Line 2..N+1: | N lines, each containing a single integer that represents the length of one supplied board |
Line N+2: | R (1 <= R <= 1023), the number of rails |
Line N+3..N+R+1: | R lines, each containing a single integer (1 <= ri <= 128) that represents the length of a single required fence rail |
SAMPLE INPUT (file fence8.in)
4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30
OUTPUT FORMAT
A single integer on a line that is the total number of fence rails that can be cut from the supplied boards. Of course, it might not be possible to cut all the possible rails from the given boards.
SAMPLE OUTPUT (file fence8.out)
7
HINTS (use them carefully!)
因为维数太多所以只能采取搜索的办法
采取dfsid的办法,即控制迭代的深度
首先把rail的数据从小到大排序,深搜判断是否能切出K个,二分答案
注意数据1=<ri <= 128 的,而他的数量高达1023个,这也就意味着会有许多相同的rail,在搜索的rail的时候是不需要考虑顺序的,若rail[i] = rail[i + 1] 则rail[i + 1]对应的
board 大于或等于rail[i]对应的board
用剩余材料做优化可以减少大量的冗余搜索,其大致思路是这样的,如果boad的总长度是board_sum,需要切割出来的rail总长度是rail_sum,那么最大的可浪费材料是max_waste,如果某一种切割方式在切割完之前已产生了waste>max_waste的浪费,那么显然这种方式是不可行的。
/*
ID:hyx34931
LANG:C++
TASK:fence8
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; int N, R;
const int MAX = ;
int ra[MAX], b[];
int sumra[MAX];
int remain[];
int sumb = ; bool dfs(int mid, int start, int limit) {
int waste = ;
if (mid == ) return true;
for (int i = ; i <= N; ++i) {
if (remain[i] < ra[]) {
waste += remain[i];
}
} if (waste > limit) return false; int s;
for (int i = start; i <= N; ++i) {
if (remain[i] >= ra[mid]) {
if (mid - >= && ra[mid] == ra[mid - ]) s = i;
else s = ;
remain[i] -= ra[mid];
if ( dfs(mid - , s, limit) ) return true;
remain[i] += ra[mid];
}
}
return false;
} void solve() { for (int i = ; i <= R; ++i) {
sumra[i] += sumra[i - ] + ra[i];
} int l = , r = R;
while (l < r) {
int mid = (l + r + ) / ;
for (int i = ; i <= N; ++i) {
remain[i] = b[i];
} //printf("f\n");
if (dfs(mid, , sumb - sumra[mid])) l = mid;
else r = mid - ;
//printf("l = %d mid = %d r = %d\n", l, mid, r);
} printf("%d\n", l);
}
int main()
{
freopen("fence8.in", "r", stdin);
//freopen("fence8.out", "w", stdout);
scanf("%d", &N);
for (int i = ; i <= N; ++i) {
scanf("%d", &b[i]);
sumb += b[i];
} scanf("%d", &R);
for (int i = ; i <= R; ++i) {
scanf("%d", &ra[i]);
} sort(ra + , ra + R + );
//sort(b + 1, b + N + 1);
solve(); return ;
}