ZOJ 3725 The Three Guys

题目连接
题意:给三个人的上半身长和下半身长,问能组成的三角形的最大面积,人不能拆分但可以弯曲。
队友的思路是枚举所有可能的三角形,六条边可能的组合有222,123,114;感觉很对,还没有想通为什么不可以这样,不过数据:10 20 30 30 20 20就会出错。正确的做法是全排列,然后判断组成的三角形是否合法。

#include
#include
#include
#include
#include
using namespace std;

int a[10],b[10];
bool check(double a, double b, double c)
{
    return ((a+b) > c) && ((a+c) > b) && ((b+c) > a);
}
double cal(double a, double b, double c)
{
    double p = (a+b+c)/2.0;
    return sqrt(p*(p-a)*(p-b)*(p-c));
}
double solve(int a[]){  
    int sum = 0;  
    for (int i = 1; i <= 6; i++)  
        sum += a[i];  
    double ans = 0,tmp;  
    for (int i = 1; i <= 6; i++)  
        for (int j = i+1; j <= 6; j++)  
            for (int k = j+1; k <= 6; k++){  
                int x = 0,y = 0,z = 0;  
                for (int l = i+1; l <= j; l++)  
                    x += a[l];  
                for (int l = j+1; l <= k; l++)  
                    y += a[l];  
                z = sum - x - y;  
                if(check(x,y,z))
                    ans = max(ans,cal(x,y,z));  
            }  
    return ans;  
}  
  

int main()
{
    while(cin >> a[1] >> a[2])
    {
       double ans=0,tmp;
       cin>>a[3]>>a[4]>>a[5]>>a[6];
       int c[7]={0,1,2,3,4,5,6};
       do{  
            int flag = 0;  
            for (int i = 1; i <= 6; i += 2)  
                if (abs(c[i]-c[i+1]) != 1){  
                    flag = 1;  
                    break;  
                }  
                if (flag)  
                    continue;  
                for (int i = 1; i <= 6; i++)  
                    b[i] = a[c[i]];  
                tmp = solve(b);  
                if (tmp > ans)  
                    ans = tmp;  
        }while (next_permutation(c+1,c+7)); 
        printf("%.12lf\n",ans);  
    }
    return 0;
}

你可能感兴趣的:(ZOJ 3725 The Three Guys)