http://www.lydsy.com/JudgeOnline/problem.php?id=1082

题意:n个给出木板,m个给出木板。可以将那m个木板锯成泥想要的长度。问最大能锯成多少个给出的n个木板。(n<=1000, m<=50)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int N=2005;
int v[N], n, m, w[N], sum[N], c[N], tot, rest; bool dfs(int dep, int last) {
if(!dep) return true;
if(rest+sum[dep]>tot) return false;
for1(i, last, n) {
if(c[i]>=w[dep]) {
c[i]-=w[dep];
if(c[i]<w[1]) rest+=c[i];
if(w[dep]==w[dep-1]) { if(dfs(dep-1, i)) return true; }
else if(dfs(dep-1, 1)) return true;
if(c[i]<w[1]) rest-=c[i];
c[i]+=w[dep];
}
}
return false;
}
bool check(int d) {
memcpy(c, v, sizeof(int)*(n+1));
rest=0;
return dfs(d, 1);
}
int main() {
read(n);
for1(i, 1, n) read(v[i]), tot+=v[i];
sort(v+1, v+1+n);
read(m);
for1(i, 1, m) read(w[i]);
sort(w+1, w+1+m);
for1(i, 1, m) sum[i]=sum[i-1]+w[i];
while(sum[m]>tot) --m;
int l=0, r=m, mid;
while(l<=r) {
mid=(l+r)>>1;
if(check(mid)) l=mid+1;
else r=mid-1;
}
print(l-1);
return 0;
}

最近被这种神题虐cry。。。这还竟然是usaco的题QAQ我竟然如此弱。。。。(我是不是写过这题?反正好像有点印象的样子。。好像又不是。。)

一开始写了个背包。。。贪心的找。。。。。。。。。。。。。。。。然后造了几个数据,,wa了。。

QAQ

膜拜题解。orz

首先我们得到的k个木板一定是在n个中最小的k个。。。(这个太显然了QAQ

我们考虑将m个提供的木材,依次从最小的放(显然先用完最短的。。。)

所以将k个木板和m个提供的木材排序后再做。。。。。

二分k,判定是否能得到。。。

那么就是暴力枚举前k个的放法,可是。。2^1000。。。。tle成翔。。。。。。。。

倒序枚举

那么考虑剪枝orz

首先如果当一个提供的木材小于了最小的所需木板,那么我们用个rest累加,当rest+sum[k]>tot时,剪掉(sum[k]表示前k个所需木板长度,tot表示提供木板的总量(其实这个剪枝还不够呢。。。因为tot是静态的,并没有变动。。。反正不改也能a))

然后继续剪枝,当k木板等于k-1木板的长度,那么我们不需要从1枚举木材了,因为此时k木板从哪个枚举说明前面的都不够了。。所以直接从k木板当前的木材枚举k-1。。

然后能水过了。。

【BZOJ】1082: [SCOI2005]栅栏(二分+dfs)的更多相关文章

  1. [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】

    题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...

  2. bzoj 1082: [SCOI2005]栅栏 题解

    1082: [SCOI2005]栅栏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 991[Submit][Status] ...

  3. 【BZOJ 1082】[SCOI2005]栅栏 二分+dfs

    对于最优解我们发现所有的最优解都可以是前多少多少个,那么我们就二分这个前多少多少个,然后用dfs去判解,我们发现在dfs的过程中如果不剪枝几乎必T,所以我们就需要一些有效的剪枝 I. 我们在枚举过程中 ...

  4. bzoj 1082: [SCOI2005]栅栏【二分+dfs】

    二分答案,dfs判断是否可行,当b[k]==b[k-1]时可以剪枝也就是后移枚举位置 #include<iostream> #include<cstdio> #include& ...

  5. bzoj 1082: [SCOI2005]栅栏

    Description 农夫约翰打算建立一个栅栏将他的牧场给围起来,因此他需要一些特定规格的木材.于是农夫约翰到木材店购 买木材.可是木材店老板说他这里只剩下少部分大规格的木板了.不过约翰可以购买这些 ...

  6. [SCOI2005]栅栏 二分+dfs

    这个题真的是太nb了,各种骚 二分答案,肯定要减最小的mid个,从大往小搜每一个木板,从大往小枚举所用的木材 当当前木材比最短的木板还短,就扔到垃圾堆里,并记录waste,当 waste+sum> ...

  7. bzoj1082: [SCOI2005]栅栏(二分答案搜索判断)

    1082: [SCOI2005]栅栏 题目:传送门 题解: 是不是一开始在想DP?本蒟蒻也是qwq,结果很nice的错了ORZ 正解:二分+搜索 我们可以先把两种木材都进行排序,那么如果需要的最大木材 ...

  8. [BZOJ1082][SCOI2005]栅栏 二分+搜索减枝

    1082: [SCOI2005]栅栏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2430  Solved: 1034[Submit][Status ...

  9. Bzoj 1085: [SCOI2005]骑士精神 (dfs)

    Bzoj 1085: [SCOI2005]骑士精神 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 dfs + 剪枝. 剪枝方法: ...

随机推荐

  1. Shortcut key for CodeBlocks

    一.not only in CodeBlocks 13.12 Undo last action Ctrl + Z //后退 Redo last action Ctrl + Shift + Z //前进 ...

  2. poj3295

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10453   Accepted: 3967 Descri ...

  3. 什么是mixin

    转自:http://guangboo.org/2013/01/28/python-mixin-programming http://en.wikipedia.org/wiki/Mixin http:/ ...

  4. 【Web】关于URL中文乱码问题

    关于URL编码                                        一.问题的由来 URL就是网址,只要上网,就一定会用到.                          ...

  5. MVC 详细说明

    .NET MVC执行过程: 1.网址路由比对 2.执行Controller与Action 3.执行View并返回结果 在使用MVC中是由IgnoreRoute()辅助方法对比成功的,会导致程序直接跳离 ...

  6. iOS UITableView 的beginUpdates和endUpdates

    在官方文档中是这样介绍beginUpdates的 Call this method if you want subsequent insertions, deletion, and selection ...

  7. Java for LeetCode 027 Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  8. poj 3750 小孩报数问题 解题报告

    题目链接:http://poj.org/problem?id=3750 约瑟夫问题,直接模拟即可. #include <iostream> #include <string> ...

  9. Servlet获取参数

    package action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet ...

  10. 二、JavaScript语言--JS基础--JavaScript进阶篇--数组

    1.什么事数组 我们知道变量用来存储数据,一个变量只能存储一个内容.假设你想存储10个人的姓名或者存储20个人的数学成绩,就需要10个或20个变量来存储,如果需要存储更多数据,那就会变的更麻烦.我们用 ...