USACO 4.1 Fence Rails

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

 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 }

USACO 4.1 Fence Rails

时间: 2024-10-18 20:13:07

USACO 4.1 Fence Rails的相关文章

USACO 6.3 Fence Rails(一道纯剪枝应用)

Fence RailsBurch, 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 s

USACO 4.1 Fence Loops

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences

USACO 4.1 Fence Loops(Floyd求最小环)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences

USACO 3.3 fence 欧拉回路

题意:求给定图的欧拉回路(每条边只走一次) 若欧拉回路存在,图中只可能有0个or2个奇数度的点. 求解时,若有奇数度的点,则必须从该点开始.否则可以从任一点开始 求解过程:dfs 1 //主程序部分 2 # circuit is a global array 3 find_euler_circuit 4 circuitpos = 0 5 find_circuit(node 1) 6 --------------------------------------------- 7 # nextnod

Fence Rails

深度迭代搜索+剪枝 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,m; 6 int a[55],value[1025]; 7 int b[55]; 8 int limit=1; 9 int space=0; 10 int maxspace=0; 11 bool cmp(int a,int b) 12 { 13 return a>

HDU 4277 USACO ORZ(暴力+双向枚举)

USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3809    Accepted Submission(s): 1264 Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastu

hdu 4277 USACO ORZ DFS

USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3581    Accepted Submission(s): 1196 Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastur

USACO 6.2 章节 你对搜索和剪枝一无所知QAQ

Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值<=128,B 问 A 们能拆分成多少个B,求最多的个数 样例 解释 A: 30=30 40=18+19+3 50=15+16+17+2 25=24 B: 15 (ok) 16 (ok) 17 (ok) 18 (ok) 19 (ok) 20 21 25 24 (ok) 30 (ok) 所以最多7个 题解 首先 如果对B 排序,假设最优个数为k个 显然 如果k个可行,那么排序后的B 的前k个可行 又 如果k

hdu 4277 USACO ORZ(dfs+剪枝)

Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.I. M. Hei, the lead cow pasture architect, is in charge of creating a