• hdu 5135(2014广州—状态dp)


    t题意:给你n条边,构造任意个三角形,一个三角形恰好只用3条边,每条边只能一次,求面积最大值

    思路:

    最开始想的是先排序从大到小取,但感觉并不怎么靠谱。

    最多12条边,所以可以求出所有可能的三角形面积,然后就不知道怎么办了- -,

    看大神的解法,状态dp,但是没想出来怎么保存状态,ヾ(。`Д´。)

    后来发现别人都是用二进制(好吧,没想到),然后dp就行了.


    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <vector>
    using namespace std;
    typedef long long ll;
    
    
    double dp[1<<13];
    int a[20];
    
    double cal(int a,int b,int c)
    {
        if(a + b <= c)
            return 0.0;
        double tmp = (a+b+c)*0.5;
        return sqrt(tmp*(tmp-a)*(tmp-b)*(tmp-c));
    }
    
    int main()
    {
        int n;
        vector<int >q;
        while(~scanf("%d",&n) && n)
        {
            for(int i = 1; i <= n; i ++)
                scanf("%d",a+i);
            sort(a+1,a+n+1);
            memset(dp,0,sizeof(dp));
            q.clear();
            for(int i = 1; i <= n; i++)
                for(int j = i+1; j <= n; j++)
                    for(int k = j+1; k <= n; k++)
                    {
                          int tt = (1<<i)|(1<<j)|(1<<k);
                          dp[tt] = cal(a[i],a[j],a[k]);
                          if(a[i] + a[j] > a[k])
                              q.push_back(tt);
                    }
            for(int i =0;i < (1<<(n+1));i++)
            {
                for(int j =0;j < q.size();j++)
                {
                    if(i & q[j])
                        continue;
                    dp[i|q[j]] = max(dp[i|q[j]],dp[i]+dp[q[j]]);
                }
            }
    
            printf("%.2f
    ", dp[(1<<(n+1))-1]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    elementUI Select 下拉框列表分两列展示
    radio切换后,表格数据记住之前选中的行
    vue 子组件和父组件生命周期顺序
    vue-router理解
    vuex理解
    实验四 决策树算法及应用
    实验三 朴素贝叶斯算法及应用
    实验二 K-近邻算法及应用
    词法分析可视化MFC
    SEO工具ahrefs各参数详解
  • 原文地址:https://www.cnblogs.com/Przz/p/5409725.html
Copyright © 2020-2023  润新知