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

简单背包!!! (此题为单组数据)

Posted by ACAccepted at 2019-03-23 11:19:59 on Problem 1948 and last updated at 2019-03-23 11:21:12
#include <cstdio>
#include <cmath>
#include <cstring>
#define max(a,b) ((a)>(b)?(a):(b))
using namespace std;

inline int read()
{
	int x=0,f=1;char c=getchar();
	while (c<'0' || c>'9'){if (c=='-')f=-1;c=getchar();}
	while (c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-48;c=getchar();}
	return x*f;
}

const int MAXN=45;
const int MAXM=805;
int n,tot,sum;
int a[MAXN];
bool f[MAXM][MAXM];

inline bool ok(int a,int b,int c)
{
	if (a<0 || b<0 || c<0)return false;
	if (a+b>c&&a+c>b&&b+c>a)return true;
	return false;
}

inline double area(int a,int b,int c)
{
	double p=(a+b+c)/2.0;
	return 100.0*sqrt(p*(p-a)*(p-b)*(p-c));
}

int main()
{
	while (~scanf("%d",&n))
	{
		tot=0;sum=0;
		memset(f,false,sizeof(f));
		for (int i=1;i<=n;i++)tot+=(a[i]=read());
		sum=tot/2;f[0][0]=true;
		for (int i=1;i<=n;i++)
			for (int j=sum;j>=0;j--)
				for (int k=sum;k>=0;k--)
				{
					if (j>=a[i])f[j][k]=f[j][k]|f[j-a[i]][k];
					if (k>=a[i])f[j][k]=f[j][k]|f[j][k-a[i]];
				}
		double ans=-1;
		for (int i=1;i<=sum;i++)
			for (int j=1;j<=sum;j++)
				if (f[i][j]&&ok(i,j,tot-i-j))
					ans=max(ans,area(i,j,tot-i-j));
		printf("%d\n",(int)ans);
	}
	return 0;
}

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