Pro.ID10177 TitleFence Rails Title链接http://10.20.2.8/oj/exercise/problem?problem_id=10177 AC3 Submit8 Ratio37.50% 时间&空间限制描述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. 输入Multiple test cases. For each case : 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 输出Description 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. Input Multiple test cases. For each case : 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 Output For each case, output 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 Input 4 Sample Output 7 Hint This is a high dimensionality multiple knapsack problem, so we just have to test the cases. Given that the search space has a high out-degree, we will use depth first search with iterative deepening in order to limit the depth of the tree. However, straight DFSID will be too slow, so some tree-pruning is necessary. Source 样例输入4 样例输出7 提示This is a high dimensionality multiple knapsack problem, so we just have to test the cases. Given that the search space has a high out-degree, we will use depth first search with iterative deepening in order to limit the depth of the tree. However, straight DFSID will be too slow, so some tree-pruning is necessary. |