1.题目描述:点击打开链接
2.解题思路:典型的状压dp题目,用集合S表示当前已经使用过的木棍的集合,那么有dp(S)=max{dp(S),S(i,j,k)+dp(S/{i,j,k})}。从小到大递推枚举集合即可。
3.代码:
#include<iostream>
#include<algorithm>
#include<cassert>
#include<string>
#include<sstream>
#include<set>
#include<bitset>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<list>
#include<complex>
#include<functional>
#define INF 0x3f3f3f3f
using namespace std;
#define me(s) memset(s,0,sizeof(s))
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
typedef long long ll;
typedef pair <int,int> P;
const int N = 15;
const int MAXN = 1<<N;
double dp[MAXN];
double a[N];
int b[N];
int n;
double getS(int x, int y, int z)
{
double p = a[x], q = a[y], r = a[z];
double half = (p + q + r) / 2;
return sqrt(half*(half - p)*(half - q)*(half - r));
}
int bitcount(int x)
{
return !x ? 0 : bitcount(x / 2) + (x & 1);
}
bool ok(int s)
{
if (bitcount(s) >= 3)return true;
return false;
}
int main()
{
while (~scanf("%d", &n) && n)
{
rep(i, n)scanf("%lf", &a[i]);
me(dp);
int ALL = 1 << n;
for (int s = 1; s<ALL; s++)
if (ok(s))
{
int cnt = 0;
for (int i = 0; i < n; i++)
if (s >> i & 1)
b[cnt++] = i;
for (int i = 0; i < cnt; i++)
for (int j = i + 1; j < cnt; j++)
for (int k = j + 1; k < cnt; k++)
{
int p = b[i], q = b[j], r = b[k];
if (a[p] + a[q]>a[r] && fabs(a[p] - a[q]) < a[r])
{
int tmp = (1 << p) | (1 << q) | (1 << r);
dp[s] = max(dp[s], dp[s&~tmp] + getS(p, q, r));
}
}
}
printf("%.2lf\n", dp[ALL - 1]);
}
}