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

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.

农民John准备建一个栅栏来围住他的牧场。他已经确定了栅栏的形状,但是他在木料方面有些问题。当地的杂货储存商扔给John一些木板,而John必须从这些木板中找出尽可能多所需的木料。

  当然,John可以切木板。因此,一个9英尺的木板可以切成一个5英尺和一个4英尺的木料 (当然也能切成3个3英尺的,等等)。John有一把梦幻之锯,因此他在切木料时,不会有木料的损失。

  所需要的木料规格都已经给定。你不必切出更多木料,那没有用。

目录

[显示

[编辑]格式

PROGRAM NAME: fence8

INPUT FORMAT:

(file fence8.in)

第1行: N (1 <= N <= 50), 表示提供的木板的数目

第2到第N+1行: 包括一个整数表示提供的一块木板的长度

第N+2行:一个整数R (1 <= R <= 1023)表示所需的木板数目

第N+3行到第N+R+1行:包括一个整数(1 <= ri <= 128)表示所需木料的长度。

OUTPUT FORMAT:

(file fence8.out)

只有一行,一个数字,表示能切出的最多的所需木料的数目。当然,并不是任何时候都能切出所有所需木料。

[编辑]SAMPLE
INPUT

4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30

[编辑]SAMPLE
OUTPUT

   7

[编辑]提示1(小心地利用它们!)

这是广度多维背包问题,所以我们必须考虑数据。给你的搜索范围有很大的下限,所以我们必须使用深搜加上迭代深化来限制树的边界。然而,直接的迭代深化会变得很慢,所以剪枝是必要的。

---------------------------------------------------分割线-----------------------------------------------------------

惭愧啊,这道搜索题做得焦头烂额,调了一个多小时,几度想放弃。还好总算成功了!

以前我在VJ上也做到过这道题,但当时一直TLE。

为了叙述方便,我们把原来的木头叫原木,后来切的叫新木。

-------------------------------------------一开始我的想法-----------------------------------------------------

首先,把原木和新木快排一遍。

每次二分枚举可以切的木块数,再去验证。

验证时,设枚举的木块数为k。由贪心思想可知,我们挑前k小的新木来切割。

事实证明,每次先用大的原木切割会更快,因为相对来说,它能分的个数更多。(想到以前NOIP2012的文化之旅,倒着搜比正着搜更快)

然后再加点小的剪枝,取新木的前缀和,如果当前试的原木可以切完所有的新木,就直接return真。

可是,到第四个点就TLE了!!!

----------------------------------------------求助互联网------------------------------------------------------

太强了!我看到NOCOW里有个题解(新木):对于切剩下的board(无法再切下rail),统计一下总和。如果这个值大于board长度的总和减去rail长度的总和,一定无解,可以剪枝。这个剪枝最关键。

加了这个剪枝之后,我就立马过了,且最慢的点也才0.11ms。

真心膜拜那位大牛!

代码:(红色为增加代码)

/*
ID:juan1973
LANG:C++
PROG:fence8
*/

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[51],b[1024],b_sum[1024],n,m,i,ans,sum,sum2;
bool check(int num,int now,int q,int limit)
{
  if (num==0) return true;
  if (q>limit) return false;
  if (b[num]!=b[num+1]) now=n;
  for (int i=now;i>0;i--)
  {
    if (a[i]>=b_sum[num]) return true;
    if (a[i]>=b[num])
    {
      a[i]-=b[num];
      if (check(num-1,i,q+(a[i]<b[1])?a[i]:0,limit)) {a[i]+=b[num];return true;}
      a[i]+=b[num];
    }
  }
  return false;
}
int erfen(int l,int r)
{
  if (l==r) return l;
  int mid=(l+r)/2+1;
  if (check(mid,n,,sum-b_sum[mid])) return erfen(mid,r);
                                     return erfen(l,mid-1);
}
int main()
{
  //freopen("fence8.in","r",stdin);
  //freopen("fence8.out","w",stdout);
  scanf("%ld",&n);
  for (i=1;i<=n;i++) {scanf("%ld",&a[i]);sum+=a[i];}
  sum2=sum;
  sort(a+1,a+n+1);
  scanf("%ld",&m);
  for (i=1;i<=m;i++) scanf("%ld",&b[i]);
  sort(b+1,b+m+1);
  for (i=1;i<=m;i++) b_sum[i]=b_sum[i-1]+b[i];
  for (i=1;i<=m;i++) {sum2-=b[i];if (sum2<0) break;}
  ans=erfen(0,i-1);
  printf("%ld\n",ans);
  //scanf("%ld",&n);
  return 0;
}

usaco training 4.1.2 Fence Rails 题解的更多相关文章

  1. usaco training 4.2.3 Job Processing 题解

    Job Processing题解 IOI'96 A factory is running a production line that requires two operations to be pe ...

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

    Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...

  3. usaco training 4.1.3 fence6 题解

    Fence Loops题解 The fences that surround Farmer Brown's collection of pastures have gotten out of cont ...

  4. USACO 4.1 Fence Rails

    Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...

  5. 关于USACO Training

    做了这么久的题目,突然发现最经典的 USACO Training 还没有做过?加速水一遍吧!我会把题解放在上面的.

  6. USACO Training Section 1.1 坏掉的项链Broken Necklace

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  7. USACO Training Section 1.1 贪婪的送礼者Greedy Gift Givers

    P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers 题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一 ...

  8. USACO Training Section 1.1 Your Ride Is Here

    题目描述 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支持者.因此,他们要用一种聪明的方案让这些小组提前知道谁会被彗星带走 ...

  9. USACO Training Section 1.2 双重回文数 Dual Palindrom

    题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做"回文数".例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就 ...

随机推荐

  1. 从Thread,ThreadPool,Task, 到async await 的基本使用方法解读

    记得很久以前的一个面试场景: 面试官:说说你对JavaScript闭包的理解吧? 我:嗯,平时都是前端工程师在写JS,我们一般只管写后端代码. 面试官:你是后端程序员啊,好吧,那问问你多线程编程的问题 ...

  2. 使用 after 伪类清除浮动

    以前清除浮动的时候总是在想要清除浮动的元素后面添加 <div style="clear:both;"></div> 或者写在br标签里面来解决,但这样会增加 ...

  3. Thrift总结(一)介绍

    这段时间,一直在整理公司的内部 rpc 服务接口,面临的一个问题就是:由于公司内部的系统由几个不同的语言编写的.C# ,java,node.js 等,如何实现这些内部系统之间的接口统一调用,确实是比较 ...

  4. node.js零基础详细教程(3):npm包管理、git github的使用

    第三章  建议学习时间4小时  课程共10章 学习方式:详细阅读,并手动实现相关代码 学习目标:此教程将教会大家 安装Node.搭建服务器.express.mysql.mongodb.编写后台业务逻辑 ...

  5. oracle 体系结构简介

    1.1.SGA(system global area) SGA是oracle Instance的基本组成部分,在示例启动是分配.是一组包含一个oracle实例的数据和控制信息的共享内存结构.主要用于存 ...

  6. WLAN高密无线网络部署的信道问题

    WIFI信号的信道有两部分,其中2.4G频段有13个左右交叠的信道(14信道只在日本使用),其中只能找出3个相互不重合的信道(具体请参考文末的链接),最常用的就是1.6.11这三个,当然也可以使用其他 ...

  7. jquery 实现滚动条下拉时无限加载的简单实例

    var lastId=0;//记录每一次加载时的最后一条记录id,跟您的排序方式有关.     var isloading = false;   $(window).bind("scroll ...

  8. MYSQL更改root password时遇到Access Denied的解决办法

    今天在公司虚拟机上装MYSQL之后需要修改root password,然而遇到这样的错误: Access denied for user 'root'@'localhost' (using passw ...

  9. Apache和PHP环境配置

    最近闲来想学习一下PHP. 工欲善其事,必先利其器.我的PHP环境配置了三遍,才安装成功. 下面就分享一下我的安装经验. 1.Apache2.4,PHP5.6,MySql5.6这些都是从官网下载的. ...

  10. jquery判断按钮是否被选中了

    <script type="text/javascript"> function genjin_view2(elm){ if($(elm).attr("che ...