BZOJ 1082 栅栏(二分+DFS剪枝)

首先,长度短的木板一定比长度长的木板容易得到,因此若要得到最多的木板,它们必定是所有木板中最短的——可以对木板排序后二分答案(用k表示)。

  判断是否合法就用搜索,但数据有点大,要用到两个剪枝。一个是若有一根木材被切开后剩下的长度比最短的木板还短则将其累加入waste,当waste+前k块木板的长度和>木材的总长度时即可return(这十分显然);二是把木材也排序,木板从大到小去搜索而木板从小到大去枚举,若当前的木板长度与上一块木板相同,则从切出上一块木板的木材开始枚举(之前的木材肯定不够用),否则从第一根开始枚举。

 

# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-8
# define MOD 1000000007
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int res=0, flag=0;
    char ch;
    if((ch=getchar())=='-') flag=1;
    else if(ch>='0'&&ch<='9') res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')  res=res*10+(ch-'0');
    return flag?-res:res;
}
void Out(int a) {
    if(a<0) {putchar('-'); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+'0');
}
const int N=105;
//Code begin...

int m, n, a[55], b[1005], sumb[1005], f[55], sum, waste, mid;

bool dfs(int x, int last)
{
    if (!x) return true;
    if (waste+sumb[mid]>sum) return false;
    int res=false;
    int t=(b[x]==b[x+1]?last:1);
    FOR(i,last,m) if (a[i]>=b[x]) {
        a[i]-=b[x];
        if (a[i]<b[1]) waste+=a[i];
        res|=dfs(x-1,b[x]==b[x-1]?i:1);
        if (res) return res;
        if (a[i]<b[1]) waste-=a[i];
        a[i]+=b[x];
    }
    return res;
}
int main ()
{
    int ma=0;
    scanf("%d",&m);
    FOR(i,1,m) scanf("%d",f+i), sum+=f[i], ma=max(ma,f[i]);
    sort(f+1,f+m+1);
    scanf("%d",&n);
    FOR(i,1,n) scanf("%d",b+i);
    sort(b+1,b+n+1);
    int l=0, r=n+1;
    FOR(i,1,n) {
       sumb[i]=sumb[i-1]+b[i];
       if (sum<sumb[i]) r=min(r,i);
       if (ma<b[i]) r=min(r,i);
    }
    if (sum>=sumb[n]&&ma>=b[n]) r=n+1;
    while (l<r) {
        mid=(l+r)>>1;
        if (l==mid) break;
        waste=0;
        FOR(i,1,m) a[i]=f[i];
        if (dfs(mid,1)) l=mid;
        else r=mid;
    }
    printf("%d\n",l);
    return 0;
}
View Code

 

posted @ 2017-03-07 15:27  free-loop  阅读(231)  评论(0编辑  收藏  举报