USACO 6.3 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!)
This is a high dimensionality multiple knapsack problem, so we just have to test the cases. Given that the search space has a high out-degree, we will use depth first search with iterative deepening in order to limit the depth of the tree. However, straight DFSID will be too slow, so some tree-pruning is necessary.
————————————————————题解
题解给了四个优化
1、某两个要砍出的木板同长,我们就总在木料的非降序中砍它们
2、有两个木料是同长的,我们总是去砍第一个
3、一个木板和木料同长,那么一定要这么砍【这个优化很迷,没有加上】
4、如果一个木料砍完后的长度小于最小的木板长,这个木料的剩余部分直接丢掉
还有个优化是二分答案求最优解,所有点0.000
一开始写的是针对每个背包往里面塞东西……应该是针对每个木板去看能不能割出来
USACO总能让人关注到一些基础算法中你啥也不会的东西……这是最有趣的……也是最痛苦的……因为发现最后真是啥也不会……
/*
ID: ivorysi
LANG: C++
PROG: fence8
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x5f5f5f5f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
int n,r;
int bag[],sp;
int wood[],sum[],mid,now;
bool used[];
void init() {
scanf("%d",&n);
siji(i,,n) {
scanf("%d",&bag[i]);
sp+=bag[i];
}
sort(bag+,bag+n+);
scanf("%d",&r);
siji(i,,r) {
scanf("%d",&wood[i]);
}
sort(wood+,wood+r+);
siji(i,,r) {
sum[i]=sum[i-]+wood[i];
}
}
bool dfs(int k,int pred) {
if(k<=) return ;
if(sp<sum[k]) return ;
sp-=wood[k];
for(int i= k<mid&&wood[k]==wood[k+] ?pred: ;i<=n;++i) {
//有两个相同长度的木板需要切,让它们以一种非降序的顺序切出来
if(bag[i]==bag[i-]) continue;//这个木料和前一个一样,那么切之后会搜出来一个一模一样的结果
if(bag[i]>=wood[k]) {
bag[i]-=wood[k];
if(bag[i]<wood[]) sp-=bag[i];//这个木料不能切除任何一块木板了
if(dfs(k-,i)) {
if(bag[i]<wood[]) sp+=bag[i];
sp+=wood[k];
bag[i]+=wood[k];
return ;
}
if(bag[i]<wood[]) sp+=bag[i];
bag[i]+=wood[k];
}
}
sp+=wood[k];
return ;
}
int binary() {
int left=,right=r;
while(left<right) {
mid=(left+right+)>>;
if(sum[mid]>sp || wood[mid]>bag[n]) {right=mid-;continue;}
if(dfs(mid,)) left=mid;
else right=mid-;
}
return left;
}
void solve() {
init();
printf("%d\n",binary());
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fence8.in","r",stdin);
freopen("fence8.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 6.3 Fence Rails(一道纯剪枝应用)的更多相关文章
- USACO 4.1 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 ...
- poj2823一道纯单调队列
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 32099 Accepted: 9526 ...
- 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 ...
- USACO 6.3 章节 你对搜索和剪枝一无所知QAQ
emmm........很久很久以前 把6.2过了 所以emmmmmm 直接跳过 ,从6.1到6.3吧 Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值 ...
- hdu 4277 USACO ORZ(dfs+剪枝)
Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
随机推荐
- redhat6下安装centos的yum源
因为redhat中的yum是收费的,未注册时不允许使用的,下面是挂载光盘后的情况,未挂载是没有yum命令.但是下面即便挂载了也是需要验证的 [root@localhost /]# yum instal ...
- Mac下crontab定时python任务
1.新建crontab_file vim输入代码*/ * * * * /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 /Us ...
- Elasticsearch技术解析与实战(四)shard&replica机制
序言 shard&replica机制 1.index包含多个shard 2.每个shard都是一个最小工作单元,承载部分数据,lucene实例,完整的建立索引和处理请求的能力 3.增减节点时, ...
- 解决组合排列问题 A (m ,n) m>=n
转载自http://blog.csdn.net/sunyujia/article/details/4124011 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取 ...
- Linux日志文件/var/log详解
更多内容推荐微信公众号,欢迎关注: 如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时 ...
- springcloud的Turbine配置监控多个服务的一些坑!!!!InstanceMonitor$MisconfiguredHostException,No message available","path":"/actuator/hystrix.stream,页面不显示服务或者一直loading
踩了几个小时坑,使用仪表盘监控单个服务的时候很容易,但是一到多个服务,瞬间坑就来了,大概碰到下面三个: 1InstanceMonitor$MisconfiguredHostException, No ...
- 【Python项目】爬取新浪微博个人用户信息页
微博用户信息爬虫 项目链接:https://github.com/RealIvyWong/WeiboCrawler/tree/master/WeiboUserInfoCrawler 1 实现功能 这个 ...
- C# 调用WSDL接口及方法
1.首先需要清楚WSDL的引用地址 如:http://XX.XX.4.146:8089/axis/services/getfileno?wsdl 上述地址的构造为 类名getfileno. 2.在.N ...
- Runtime - Associated Objects (关联对象) 的实现原理
主要围绕3个方面说明runtime-Associated Objects (关联对象) 1. 使用场景 2.如何使用 3.底层实现 3.1 实现原理 3.2 关联对象被存储在什么地方,是不是存放在被 ...
- C 数据结构堆
引言 - 数据结构堆 堆结构都很耳熟, 从堆排序到优先级队列, 我们总会看见它的身影. 相关的资料太多了, 堆 - https://zh.wikipedia.org/wiki/%E5%A0%86%E7 ...