您的位置:首页 > 其它

POJ 3628 - Bookshelf 2

2012-09-05 12:39 197 查看
题目地址: http://poj.org/problem?id=3628

暴力枚举。

没什么好说的。。。

也可以分两部分,然后二分做,可以节约很多时间。

#include<cstdio>
#include<cstring>

int p[23],s[23];

int main(){
int n,i,b,sum,res,tmp,t;
scanf("%d%d",&n,&b);
for(i=0;i<n;i++){
scanf("%d",&s[i]);
p[i]=0;
}
p
=0;
p[0]=1;
res=1000000000;
while(!p
){
sum=0;
for(i=0;i<n;i++)
if(p[i]) sum+=s[i];
if(sum>=b){
tmp=sum-b;
if(tmp<res) res=tmp;
}
t=0;
p[0]++;
while(p[t]>1){
p[t+1]++;
p[t]=0;
t++;
}
}
printf("%d\n",res);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: