题意 给你n个边,让你自由组合三角形,让总面积最大,边不能重用,不能拼接。总边数不超过12,边长不超过100。
这题说来都是泪...这题数据很水,其实直接贪心就能过...我们比赛的时候也想了下贪心,不过一想不对啊,就没尝试...结果暴力写碎了。。。算了,伤心事不提了....
思路1 暴力枚举,再用海伦公式,状态数C(12,3)*C(9,3)*C(6,3)*C(3,3),这样状态其实有重复,不过算一下复杂度够,就没再优化。这个方法,递归写起来很复杂,比赛的时候一个函数递归没调出来,回来用了两个函数嵌套递归,也是非常复杂,不过最让人心碎的是还T了.....大概是递归调用和不停地开vector太耗时了...没有办法,限制了递归了次数,终于A了....
思路2 状压DP,参考了大神们的解题报告,大体思路和暴力其实差不了太多,不过这是先把所有边的组合枚举出来,DP的状态是所有边的子集,DP[i]表示在i这个子集下,所得到的最大面积,状态转移就参见代码吧,
思路1代码:
以下代码,写的不漂亮,各种优化破坏了结构...不过也是个两函数嵌套递归的例子,就贴出来看看吧...
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<double> vd;
vd vec;
vd now[5];
int mark[5][30];
int n;
double max1;
void go(int dep,double sum,vd& sel,vd& yuan);
double fuck(vd& sel)
{
double p = 0.0,ret = 1.0;
p = 0.5 * (sel[0] + sel[1] + sel[2]);
ret = sqrt((p-sel[0])*(p-sel[1])*(p-sel[2])*p);
return ret;
}
bool judge(vd& a)
{
if(a[0] + a[1] < a[2])
return 0;
if(a[0] + a[2] < a[1])
return 0;
if(a[1] + a[2] < a[0])
return 0;
return 1;
}
int cnt = 0;
const int MAXX = 100000;
const int k = 3;
int goDep;
double sum1;
void zuHe(int pre,vd& sel,vd& yuan)
{
int dep = sel.size();
int p = yuan.size();
if(cnt > MAXX)
return;
if(dep == k)
{
go(goDep,sum1,sel,yuan);
return;
}
for(int i = pre;i<=p-k+dep;i++)
{
sel.push_back(yuan[i]);
mark[goDep][i] = 1;
zuHe(i+1,sel,yuan);
mark[goDep][i] = 0;
sel.pop_back();
}
}
void go(int dep,double sum,vd& sel,vd& yuan)
{
vd newyuan;
double cao;
if(dep != 0 && judge(sel) == 0)
cao = 0.0;
else
cao = dep ? fuck(sel) : 0.0;
if(n-3*dep < 3)
{
cnt++;
max1 = max(sum+cao,max1);
return;
}
for(int i = 0;i<yuan.size();i++)
{
if(mark[dep][i] == 0)
newyuan.push_back(yuan[i]);
}
goDep = dep+1;
sum1 = sum+cao;
zuHe(0,now[dep],newyuan);
goDep = dep;
sum1 = sum;
}
bool cmp(double x,double y)
{
return x>y;
}
int main()
{
int i,j;
// freopen("data.out","w",stdout);
while(scanf("%d",&n) == 1 && n)
{
vec.clear();
cnt = 0;
max1 = 0.0;
memset(mark,0,sizeof(mark));
for(i=0;i<5;i++)
now[i].clear();
for(i=0;i<n;i++)
{
double cao;
scanf("%lf",&cao);
vec.push_back(cao);
}
sort(vec.begin(),vec.end(),cmp);
if(vec[n-1] < vec[n-3] - vec[n-2])
{
vec.pop_back();
n--;
}
if(vec[0] > vec[2] + vec[1])
{
vec.erase(vec.begin());
n--;
}
go(0,0.0,now[0],vec);
printf("%.2f\n",max1);
}
return 0;
}
思路2代码:
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
double a[15];
vector<int> stage;
double dp[1<<12];
double f(double a,double b,double c)
{
double p = (a+b+c)/2.0;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
int main()
{
int i,j,k;
while(scanf("%d",&n) == 1 && n)
{
memset(dp,0,sizeof(dp));
stage.clear();
for(i=0;i<n;i++)
scanf("%lf",&a[i]);
//保证有序,是为了在判断能否构成三角形时便于判断
sort(a,a+n);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
for(k=j+1;k<n;k++)
{
if(a[i] + a[j] <= a[k])
continue;
stage.push_back((1<<i) | (1<<j) | (1<<k));
dp[(1<<i) | (1<<j) | (1<<k)] = f(a[i],a[j],a[k]);
}
}
}
for(i=0;i<(1<<n);i++)
{
for(j=0;j<stage.size();j++)
{
if((i&stage[j]) != 0)
continue;
dp[i | stage[j]] = max(dp[i | stage[j]],dp[i] + dp[stage[j]]);
}
}
printf("%.2f\n",dp[(1<<n)-1]);
}
return 0;
}