| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
贴个AC的01背包代码,方便后人#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
bool f[1601][1601],g[1601][1601];
int n,tot,l1,l2,l3,ans;
int a[41];
double p;
bool able(int x,int y,int z)
{
if (x+y<=z) return false;
if (x+z<=y) return false;
if (y+z<=x) return false;
return true;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]),tot+=a[i];
memset(f,false,sizeof(f));
f[0][0]=true;
for (int i=1;i<=n;i++)
{
memset(g,false,sizeof(g));
for (int j=0;j<=tot/2;j++)
for (int k=j;k<=tot/2;k++)
{
if (j-a[i]>=0) g[j][k]=g[j][k] or f[j-a[i]][k];
if (k-a[i]>=0) g[j][k]=g[j][k] or f[j][k-a[i]];
}
for (int j=0;j<=tot/2;j++)
for (int k=j;k<=tot/2;k++)
f[j][k]=f[j][k] or g[j][k];
}
ans=-1;
for (int i=0;i<=tot/2;i++)
for (int j=i;j<=tot/2;j++)
if (able(i,j,tot-i-j) && f[i][j])
{
l1=i;l2=j;l3=tot-i-j;
p=(l1+l2+l3)/2.0;
if (sqrt(p*(p-l1)*(p-l2)*(p-l3))*100>ans) ans=sqrt(p*(p-l1)*(p-l2)*(p-l3))*100;
}
printf("%d",ans);
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator