zoukankan      html  css  js  c++  java
  • P1284 三角形牧场

    题目描述

    和所有人一样,奶牛喜欢变化。它们正在设想新造型的牧场。奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场。她拥有N(3≤N≤40)块木板,每块的长度Li(1≤Li≤40)都是整数,她想用所有的木板围成一个三角形使得牧场面积最大。

    请帮助Hei小姐构造这样的牧场,并计算出这个最大牧场的面积。

    输入输出格式

    输入格式:

    第1行:一个整数N

    第2..N+1行:每行包含一个整数,即是木板长度。

    输出格式:

    仅一个整数:最大牧场面积乘以100然后舍尾的结果。如果无法构建,输出-1。

    输入输出样例

    输入样例#1:
    5
    1
    1
    3
    3
    4
    
    输出样例#1:
    692
    

    说明

    样例解释:692=舍尾后的(100×三角形面积),此三角形为等边三角形,边长为4。


    我们把三角形的三条边当做搜索的变量。
    那么当我们知道其中两个边的长度的话,就可以推出第三条边的长度。
    对于每一个木棒,我们都有三种策略
    1.加到第一条边
    2.加到第二条边
    3.加到第三条边。
    然后暴力记忆化搜索就好了!

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<queue>
     6 #include<algorithm>
     7 #include<map>
     8 #define lli long long int 
     9 using namespace std;
    10 const int MAXN=10000001;
    11 void read(int &n)
    12 {
    13     char c='+';int x=0;bool flag=0;
    14     while(c<'0'||c>'9')
    15     {c=getchar();if(c=='-')flag=1;}
    16     while(c>='0'&&c<='9')
    17     {x=x*10+(c-48);c=getchar();}
    18     flag==1?n=-x:n=x;
    19 }
    20 int n;
    21 int fi,se;
    22 int vis[MAXN];
    23 int sum=0;
    24 int a[MAXN];
    25 int happen[MAXN];
    26 double ans=-1;
    27 map<string,bool>mp;
    28 double calc(double yi,double er,double san)
    29 {
    30     if(min(min(yi,er),min(er,san))+min(min(yi,er),min(er,san))>max(max(yi,er),max(er,san)))
    31     {
    32         double p=(yi+er+san)/2;
    33         return sqrt(p*(p-yi)*(p-er)*(p-san));    
    34     }
    35     else
    36     return -1;
    37 }
    38 int comp(const int a,const int b)
    39 {
    40     return a<b;
    41 }
    42 void dfs(int yi,int er,int san)
    43 {
    44     if(happen[yi*1500+er*150+san])
    45         return ;
    46     happen[yi*1500+er*150+san]=1;
    47     if(yi+er+san==sum&&yi!=0&&er!=0&&san!=0)
    48     {
    49         double hh=calc(yi,er,san);
    50         if(hh>ans)
    51         ans=calc(yi,er,san);
    52     }
    53         
    54     for(int i=1;i<=n;i++)
    55     {
    56         if(vis[i]==0)
    57         {
    58             vis[i]=1;
    59             dfs(yi+a[i],er,sum-(yi+a[i]+er));
    60             dfs(yi,er+a[i],sum-(yi+er+a[i]));
    61             vis[i]=0;
    62             dfs(yi,er,sum-(yi+er));
    63         }
    64     }
    65 }
    66 int main()
    67 {
    68     
    69     read(n);
    70     for(int i=1;i<=n;i++)
    71     {
    72         read(a[i]); sum+=a[i];    
    73     }
    74     sort(a+1,a+n+1,comp);// 排序是为了方便调试 
    75     dfs(0,0,0);
    76     if(ans==-1)
    77     { printf("-1"); return 0;}
    78     ans=ans*100.0;
    79     printf("%d",(int)ans);
    80     return 0;
    81 }
  • 相关阅读:
    shell十三问?
    OS + Linux nmon / nmon analyser / nmon_analyser_v52_1.zip
    nGrinder windows agent / linux agent
    java Base64
    SearchServer Elasticsearch Cluster / kibana
    db mysql / mysql cluster 5.7.19 / my.cnf / thread_pool_stall_limit
    Mininet与真实网络链接的方法
    Install ProcessMaker 3.1 or 3.2 in CentOS/RHEL 7
    软件版本GA,RC,alpha,beta,Build 含义
    paper-9-Research and Implementation of MultiPath TCP on Mobile Smart Deviceses
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/7106873.html
Copyright © 2011-2022 走看看