###### [SCOI2005]栅栏

Description

Input

Output

Sample Input

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

Sample Output

7

#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,l,r,mid,ans,d,sum;
int a[11000],b[11000],v[10100];
bool dfs(int k,int t){
if(k==0)return true;
if(d+v[mid]>sum)return false;
bool bk;
for(int i=t;i<=m;i++){
if(b[i]>=a[k]){
b[i]-=a[k];
if(b[i]<a[1])d+=b[i];
if(a[k-1]==a[k])bk=dfs(k-1,i);
else bk=dfs(k-1,1);
if(b[i]<a[1])d-=b[i];
b[i]+=a[k];
if(bk==true)return true;
}
}
return false;
}
int main(){
sum=0;
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d",&b[i]);sum+=b[i];
}
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(a+1,a+n+1);sort(b+1,b+m+1);
v[0]=0;for(int i=1;i<=n;i++)v[i]=v[i-1]+a[i];
while(v[n]>sum)n--;
l=1;r=n;ans=0;d=0;
while(l<=r){
mid=(l+r)/2;
if(dfs(mid,1)==true){
l=mid+1;ans=mid;
}
else r=mid-1;
}
printf("%d\n",ans);
return 0;
}

#### BZOJ 1082: [SCOI2005]栅栏 DFS,剪枝，二分答案

2017-01-30 16:57:14

#### 1082: [SCOI2005]栅栏

2017-07-12 20:39:02

#### bzoj1082[SCOI2005]栅栏

2016-08-15 17:42:03

#### BZOJ1082: [SCOI2005]栅栏

2017-05-25 09:14:37

#### [SCOI2005] 最大子矩阵

2016-06-16 20:42:41

#### [SCOI2005]栅栏 二分+dfs

2017-09-17 15:21:36

#### [BZOJ1082][SCOI2005]栅栏

2016-08-29 14:37:50

#### 【bzoj1082】[SCOI2005]栅栏

2016-08-29 10:38:01

#### 【BZOJ 1082】 [SCOI2005]栅栏

2015-03-05 21:32:08

#### 洛谷 P1896 [SCOI2005]互不侵犯King

2017-03-30 15:02:27

[SCOI2005]栅栏