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!)
因为维数太多所以只能采取搜索的办法
采取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 ;
}
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 ...
- usaco training 4.1.2 Fence Rails 题解
Fence Rails题解 Burch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of h ...
- USACO 3.3 fence 欧拉回路
题意:求给定图的欧拉回路(每条边只走一次) 若欧拉回路存在,图中只可能有0个or2个奇数度的点. 求解时,若有奇数度的点,则必须从该点开始.否则可以从任一点开始 求解过程:dfs //主程序部分 # ...
- USACO 4.1 Fence Loops(Floyd求最小环)
Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...
- USACO 4.1 Fence Loops
Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...
- hdu4277 USACO ORZ
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- hdu 4277 USACO ORZ dfs+hash
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- hdu 4277 USACO ORZ DFS
USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- volley基本使用方法
用volley訪问server数据,不用自己额外开线程.以下样例为訪问JSONObject类型的数据,详细使用方法看代码: 首先得有volley的jar包,假设自己没有.去github上下载,然后自己 ...
- WebGIS开发技术杂谈
WebGIS项目的开发主要是B/S架构.最流行的是clientjavascript,server端java. 另外还有flexclient. client主要完毕用户交互.向server端发送请求并传 ...
- 本地项目上传虚拟机的gitlab
前提:在虚拟机安装了gitlab服务,并且本机可以访问到虚拟机的gitlab 自己本机项目上传到gitlab 1.先在gitlab上建立项目 拷贝项目地址: http://192.168.1.105/ ...
- 保存Activity的状态
一般来说, 调用onPause()和onStop()方法后的activity实例仍然存在于内存中, activity的全部信息和状态数据不会消失, 当activity又一次回到前台之后, 全部的改变都 ...
- Linux C语言头文件搜索路径
本文介绍在linux中头文件的搜索路径,也就是说你通过include指定的头文件,linux下的gcc编译器它是怎么找到它的呢.在此之前,先了解一个基本概念. 头文件是一种文本文件,使用文本编辑器将代 ...
- poj--2031--Building a Space Station(prime)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6635 Accepte ...
- 79.员工薪水报表 Extjs 页面
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" ...
- javascript 原型(prototype 、__proto__、函数、对象)
一.类型 1.JavaScript中分为值类型(string/boolean/null/number/undefind).引用类型(数组.对象.函数): 2.数组.函数.对象都是对象: 对象是由函数创 ...
- ROS-URDF-Xacro
前言:Xacro是一种宏语言,允许代码复用,使用Xacro可以减少URDF文件中的代码量. 参考自:http://wiki.ros.org/urdf/Tutorials/Using%20Xacro%2 ...
- 脑洞大开加偏执人格——可持久化treap版的Link Cut Tree
一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现 ...