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. python 计算两个日期相差多少个月

    近期,由于业务需要计算两个日期之前相差多少个月.我在网上找了很久,结果发现万能的python,居然没有一个模块计算两个日期的月数,像Java.C#之类的高级语言,都会有(date1-date2).mo ...

  2. C#访问ORALCE数据库

    随着时间的推移知识也在更新,原来可用的技术也会被淘汰或更新. framework4.0开始不再支持System.Data.OracleClient了,但是令人欣慰的是ORACLE公司自己出了一个Ora ...

  3. Python爬虫02——贴吧图片爬虫V2.0

    Python小爬虫——贴吧图片爬虫V2.0 贴吧图片爬虫进阶:在上次的第一个小爬虫过后,用了几次发现每爬一个帖子,都要自己手动输入帖子链接,WTF这程序简直反人类!不行了不行了得改进改进. 思路: 贴 ...

  4. Swift 入门之简单语法(二)

    可选项判断 由于可选项的内容可能为 nil,而一旦为 nil 则不允许参与计算 因此使用中,经常需要判断可选项的内容是否为 nil 单个可选项判断 let url = NSURL(string: &q ...

  5. JavaSE教程-03Java中分支语句与四种进制转换-思维导图

    思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 2)右击在新标签中打开放大查看 if语句 a) if语句 基本语法结构: if(关系表达式) { 基本语句体 } 执行流程: 首先判断 ...

  6. 初识Java-IO流

    1.定义: 流是一种抽象概念,它代表了数据的无结构化传递.数据流(Stream)是指数据通信的通道. 2.流的分类: 1)按流向分 输入流:从数据源到程序中的流 输出流:从程序到数据源的流 2)按数据 ...

  7. Publishing failed with multiple errors.问题解决

    问题:Publishing failed with multiple errors.(发布失败与多个错误) 原因:项目工程文件删除,但eclipse里面仍显示存在. 解决方案:刷新项目工程,重新部署, ...

  8. hibernate配置三步走

    1.导入有hibernate的required和jpa的jar包,数据库驱动jar包,日志jar包 2.创建实体类,必须有一个属性有唯一的值 package com.hui.hibernate; pu ...

  9. 代码管理器 TFS2013

    多人开发代码管理器肯定是少不了的,出于项目需要在服务器上装了tfs2013用于代码管理,既然用vs进行开发自然选择微软自家的tfs.记录下安装和使用起来的过程. 安装 TFS2013(Team Fou ...

  10. Ubuntu17.04配置LNMP(Nginx+PHP7+MySQL)简单教程 快速 易学 简单易懂

    我安装的是当前最新的Ubuntu版本17.04,在虚拟机中先试用一下,如果没有什么不稳定的现象,准备以后作为主力操作系统 Ubuntu属于Debian系的Linux系统,拥有着一个很NB的软件包管理器 ...