首先,长度短的木板一定比长度长的木板容易得到,因此若要得到最多的木板,它们必定是所有木板中最短的——可以对木板排序后二分答案(用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-
# define MOD
# define INF
# 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<<,l,mid
# define rch p<<|,mid+,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=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int m, n, a[], b[], sumb[], f[], 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+]?last:);
FOR(i,last,m) if (a[i]>=b[x]) {
a[i]-=b[x];
if (a[i]<b[]) waste+=a[i];
res|=dfs(x-,b[x]==b[x-]?i:);
if (res) return res;
if (a[i]<b[]) waste-=a[i];
a[i]+=b[x];
}
return res;
}
int main ()
{
int ma=;
scanf("%d",&m);
FOR(i,,m) scanf("%d",f+i), sum+=f[i], ma=max(ma,f[i]);
sort(f+,f+m+);
scanf("%d",&n);
FOR(i,,n) scanf("%d",b+i);
sort(b+,b+n+);
int l=, r=n+;
FOR(i,,n) {
sumb[i]=sumb[i-]+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+;
while (l<r) {
mid=(l+r)>>;
if (l==mid) break;
waste=;
FOR(i,,m) a[i]=f[i];
if (dfs(mid,)) l=mid;
else r=mid;
}
printf("%d\n",l);
return ;
}
05-14 23:25