Usaco 4.1 fence rails

Source: Internet
Author: User
Fence rails
Burch, kolstad, and schrijvers

Farmer John is trying to erect a fence und 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 much 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', 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: fence8input 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)
4304050251015161718192021252430
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

 

Because there are too many dimensions, you can only search for them.

The dfsid method is used to control the depth of iteration.

First, sort the rail data from small to large, and perform a deep search to determine whether K records can be cut out.

Note that data 1 = <RI <= 128, and the number of such data items is as high as 1023, which means there will be many identical rail, when searching for rail, you do not need to consider the order. If rail [I] = rail [I + 1], then rail [I + 1] corresponds

Board is greater than or equal to the Board corresponding to rail [I]

Optimization with remaining materials can reduce a large number of redundant searches. The general idea is this. If the total length of BOAD is board_sum, the total length of rail to be cut out is rail_sum, max_waste is the biggest material waste. If a cutting method has generated a waste of waste> max_waste before the cutting, it is obviously not feasible.

 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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.