poj 1948 Triangular Pastures

题目链接

题意:给出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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值