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

 /*
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 ;
}

最新文章

  1. 推荐:图片轮播插件Nivo Slider
  2. MFC 字符串类CString 源代码
  3. LINQ的Intersect方法
  4. LinkedList其实就那么一回事儿之源码分析
  5. Java学习笔记(十九)——Java 日志记录 AND log4j
  6. Mixing Delphi and C++(相互调用)
  7. iOS - Swift SingleClass 单例类
  8. NOIP 2014 普及组 T4 子矩阵
  9. Apache Rewrite url重定向功能的简单配置
  10. canvas个人总结
  11. (转)android客户端从服务器端获取json数据并解析的实现代码
  12. web后台获取不到session中的值(loading sessions from persistent storage),后改用JS传值
  13. centos 7安装mysql5.5
  14. 【RequireJS】requireJS的基础知识
  15. 数据库Oracle
  16. 转载----开发者大杀器 —— 刨根问底,揪出 Android App 耗电的元凶代码
  17. xargs命令的使用
  18. mybatis打印完整的sql
  19. R(2) sample
  20. 【MFC】转:在CHtmlView中判断页面加载完成

热门文章

  1. HDU 5266 bc# 43 LCA+跳表
  2. DBCP数据源使用
  3. LESS2CSS for sumlime text2
  4. [CTSC 2008] 祭祀
  5. python 函数参数的传递(参数带星号的说明)
  6. Human Gene Functions(dp)
  7. Shuffle&#39;m Up(串)
  8. [Apple开发者帐户帮助]六、配置应用服务(3)创建地图标识符和私钥
  9. NYOJ999 师傅又被妖怪抓走了
  10. Cracking the Coding Interview 8.7