码迷,mamicode.com
首页 > 其他好文 > 详细

USACO 4.1 Fence Rails

时间:2014-07-16 00:08:30      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   strong   os   

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!)

       HINT 1   

 

因为维数太多所以只能采取搜索的办法

采取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的浪费,那么显然这种方式是不可行的。

bubuko.com,布布扣
 1 /*
 2 ID:hyx34931
 3 LANG:C++
 4 TASK:fence8
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <vector>
11 
12 using namespace std;
13 
14 int N, R;
15 const int MAX = 1100;
16 int ra[MAX], b[100];
17 int sumra[MAX];
18 int remain[100];
19 int sumb = 0;
20 
21 bool dfs(int mid, int start, int limit) {
22         int waste = 0;
23         if (mid == 0) return true;
24         for (int i = 1; i <= N; ++i) {
25                 if (remain[i] < ra[1]) {
26                         waste += remain[i];
27                 }
28         }
29 
30 
31         if (waste > limit) return false;
32 
33         int s;
34         for (int i = start; i <= N; ++i) {
35                 if (remain[i] >= ra[mid]) {
36                         if (mid - 1 >= 1 && ra[mid] == ra[mid - 1]) s = i;
37                         else s = 1;
38                         remain[i] -= ra[mid];
39                         if ( dfs(mid - 1, s, limit) ) return true;
40                         remain[i] += ra[mid];
41                 }
42         }
43         return false;
44 }
45 
46 void solve() {
47 
48         for (int i = 1; i <= R; ++i) {
49                 sumra[i] += sumra[i - 1] + ra[i];
50         }
51 
52 
53         int l = 0, r = R;
54         while (l < r) {
55                 int mid = (l + r + 1) / 2;
56                 for (int i = 1; i <= N; ++i) {
57                         remain[i] = b[i];
58                 }
59 
60                 //printf("f\n");
61                 if (dfs(mid, 1, sumb - sumra[mid])) l = mid;
62                 else r = mid - 1;
63                 //printf("l = %d mid = %d r = %d\n", l, mid, r);
64         }
65 
66         printf("%d\n", l);
67 }
68 int main()
69 {
70     freopen("fence8.in", "r", stdin);
71     //freopen("fence8.out", "w", stdout);
72     scanf("%d", &N);
73     for (int i = 1; i <= N; ++i) {
74             scanf("%d", &b[i]);
75             sumb += b[i];
76     }
77 
78     scanf("%d", &R);
79     for (int i = 1; i <= R; ++i) {
80             scanf("%d", &ra[i]);
81     }
82 
83     sort(ra + 1, ra + R + 1);
84     //sort(b + 1, b + N + 1);
85     solve();
86 
87     return 0;
88 }
View Code

 

 

USACO 4.1 Fence Rails,布布扣,bubuko.com

USACO 4.1 Fence Rails

标签:style   blog   http   color   strong   os   

原文地址:http://www.cnblogs.com/hyxsolitude/p/3844908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!