题意:给出n~40个木棍的长度l~40,要求用这些木棍组成一个三角形,必须全部用上,求最大面积,不能组成输出-1。
题解:组成一个三角形,枚举一条边有多长,还要枚举另一条边有多长,还要看存在不存在,显然就是dp,而且复杂度也行(第三条边就是tot-i-j)就行了。重点注意和的上限是1600,但是只有一个能够大于800,不可能两个同时大于800,可以作为dp的值,所以,dp【800】【800】就行了。算面积就是海伦公式。
重点:枚举有多长,发现两条边就够了,01背包,然后可以减少到800。海伦公式:S=√p(p-a)(p-b)(p-c) 内接圆四边行:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(int i = a;i < b;i++)
#define REP_D(i, a, b) for(int i = a;i <= b;i++)
typedef long long ll;
using namespace std;
const int maxn = 2e3 + 10;
int dp[maxn][maxn], n, l[maxn];
int tot, t;//tot只有和的一半大
int area(int a, int b, int c)
{
double p = (a + 0.0 + b + c)/2.0;
double tmp = sqrt((p-a)*(p-b)*(p-c)*p)*100;
int ans = tmp + 1e-10;
return ans;
}
void getDp()//dp看存在不。
{
memset(dp, -1, sizeof(dp));
dp[0][0] = 1;
REP_D(i, 1, n)
{
for(int a = t;a >= 0;a--)
{
for(int b = t;b >= 0;b--)
{
if(dp[a][b] == -1)
{
if(a >= l[i]&&dp[a-l[i]][b]!=-1)
{
dp[a][b] = 1;
}
else if(b >= l[i]&&dp[a][b-l[i]]!=-1)
{
dp[a][b] = 1;
}
}
}
}
}
}
int check(int a,int b, int c)
{
return ((a + b > c)&&(a+c>b)&&(b + c>a));
}
void solve()
{
getDp();//所有可能
int ans = -1;
for(int i = 1;i <= t ;i++)
{
for(int j = 1;j <= t ;j++)
{
if(i + j < tot && dp[i][j] != -1&&check(i, j, tot - i - j))//重点,要考虑能不能构成三角形
{
ans = max(ans, area(i, j, tot - i - j));
}
}
}
printf("%d\n", ans);
}
int main()
{
// freopen("3Cin.txt", "r", stdin);
//freopen("3Cout.txt", "w", stdout);
while(scanf("%d", &n) != EOF)
{
tot = 0;
REP_D(i, 1, n)
{
scanf("%d", &l[i]);
tot += l[i];
}
t = tot/2 + 1;
solve();
}
return 0;
}