Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴个AC的01背包代码,方便后人

Posted by Tanko at 2016-01-08 08:00:29 on Problem 1948
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator