Description

Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite. 



I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N (3 <= N <= 40) fence segments (each of integer length Li (1 <= Li <= 40) and must arrange them into a triangular
pasture with the largest grazing area. Ms. Hei must use all the rails to create three sides of non-zero length. 



Help Ms. Hei convince the rest of the herd that plenty of grazing land will be available.Calculate the largest area that may be enclosed with a supplied set of fence segments. 

Input

* Line 1: A single integer N 



* Lines 2..N+1: N lines, each with a single integer representing one fence segment's length. The lengths are not necessarily unique. 

Output

A single line with the integer that is the truncated integer representation of the largest possible enclosed area multiplied by 100. Output -1 if no triangle of positive area may be constructed. 

此题就是二维DP的模板题,然而等我兴冲冲地打完基础代码后,发现竟然WA了!
无奈自己下数据,最后发现数据一大我的答案就偏小一些。这到底是怎么了??
后来SYF大牛途径我的座位,直接点拨那个经典错误——精度问题。

代码:
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;double ans;
long now,i,j,k,sum,len=0,n,a[41];
bool f[1601][1601];
double count(long x,long y)
{
  long z=len-x-y;
  if (x+y<=z&&x+z<=y&&y+z<=x) return 0;
  double p=(x+y+z)*1.0/2;
  return (sqrt(p*(p-x)*(p-y)*(p-z)));
}
int main()
{
  //freopen("pasture.in","r",stdin);
  //freopen("pasture.out","w",stdout);
  scanf("%ld",&n);
  for (i=1;i<=n;i++)
    {
      scanf("%ld",&a[i]);
      len+=a[i];
    }
  memset(f,0,sizeof(f));
  f[0][0]=true;sum=0;ans=0;
  for (i=1;i<=n;i++)
  {
    for (j=sum;j>=0;j--)
      for (k=sum;k>=0;k--)
        if (f[j][k])
        {
          f[j+a[i]][k]=true;
          f[j][k+a[i]]=true;
        }
    sum+=a[i];
  }
  for (i=1;i<=sum;i++)
    for (j=1;j<=sum;j++)
      if (f[i][j])
        {
          ans=max(ans,count(i,j));
        }
  now=long(ans*100);
  if (now==0) printf("-1");
  else printf("%ld",now);
  //scanf("%ld",&n);
  return 0;
}


poj 1948 Triangular Pastures 小结的更多相关文章

  1. [POJ] 1948 Triangular Pastures (DP)

    题目地址:http://poj.org/problem?id=1948 题目大意: 给N条边,把这些边组成一个三角形,问面积最大是多少?必须把所有边都用上. 解题思路: 根据题意周长c已知,求组合三边 ...

  2. POJ 1948 Triangular Pastures【二维01背包】

    题意:给出n条边,用这n条边构成一个三角形,求三角形的最大面积. 先求面积,用海伦公式,s=sqrt(p*(p-a)*(p-b)*(p-c)),其中a,b,c分别为三角形的三条边,p为三角形的半周长, ...

  3. POJ 1948 Triangular Pastures

    题意: 把很多单独的线段重新组合成一个三角形,使得三角形面积最大(所有的线段都必须用上). 思路: 三角形的任意一条边的边长不能超过周长的一半,只需要用dp枚举两条边j,k,剩下的一条边长为tot  ...

  4. Triangular Pastures POJ - 1948

    Triangular Pastures POJ - 1948 sum表示木条的总长.a[i]表示第i根木条长度.ans[i][j][k]表示用前i条木条,摆成两条长度分别为j和k的边是否可能. 那么a ...

  5. POJ1948 Triangular Pastures

    POJ1948 Triangular Pastures #include <iostream> #include <cmath> using namespace std; ; ...

  6. Triangular Pastures (二维01背包)

    描述Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectang ...

  7. POJ 3086 Triangular Sums (ZOJ 2773)

    http://poj.org/problem?id=3086 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1773 让你计算两 ...

  8. poj 1948二维01背包

    题意:给出不多于40个小棍的长度,求出用所有小棍组成的三角形的最大面积. 思路:三角形3边求面积,海伦公式:p=(a+b+c)/2;S=p*(p-a)*(p-b)*(p-c);因为最大周长为1600  ...

  9. POJ - 1948 二维01背包

    T了两发,DP方程很简单粗暴 dp[i][j][k]:用前i物品使得容量分别为j和k的背包恰好装满 背包的调用只需一次即可,第一次T就是每次check都丧心病狂地背包一次 对于sum的枚举,其实i j ...

随机推荐

  1. JDBC进阶

    PreparedStatement的使用: conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?" ...

  2. AsyncTask和Handler

    AsyncTask实现的原理和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口 ...

  3. 2017 UESTC Training for Graph Theory

    图论姿势太弱,这套题做了好久.. A:枚举最短那条边,然后最小生成树那种操作,1 和 n 联通就算答案 B:考虑到假如我们能凑出x的话,那很明显我们也能凑出任意数表示x + ai,考虑选取一个ai,然 ...

  4. 如何打一个FatJar(uber-jar)

    如何打一个FatJar(uber-jar) FatJar也就叫做UberJar,是一种可执行的Jar包(Executable Jar).FatJar和普通的jar不同在于它包含了依赖的jar包. 1. ...

  5. python3 爬 妹子图

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 Beautiful Soup 4 通过PyP ...

  6. VueJs生产环境部署

    VueJs为客户端语言,所以部署的时候是不需要基于nodejs或其他服务器运行环境,只需要像其他静态站点的方式发布就可以了,下面介绍一下VueJs具体发布的流程还有需要注意的点. 先来看VueJs最终 ...

  7. (原创) Maven查看JAR包的依赖关系

    如果是用命令行,可进入项目所在目录,然后输入: mvn dependency:tree ,来查看jar包依赖关系. 另外还可以在eclipse操作,如下图所示: 点击run后,开始输出JAR包依赖树. ...

  8. Redis数据类型之Hash(二)

    前言: Redis hash是一个String类型的field和value的映射表.添加.删除操作复杂度平均为O(1),为什么是平均呢?因为Hash的内部结构包含zipmap和hash两种.hash特 ...

  9. 什么是 html 标签,html 实体

    为什么需要转换 更简了,因为有时候我们需要在浏览器页面中显示 html 标签,然而直接输出<script>alert(1)</script>,在浏览页面时将会被当作 html ...

  10. 关于table 冻结 标头及列---js控制方法

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...