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. HourRank 20

    第一题略 第二题组合数学 s1 = min(cnt['a'],cnt['b']), s2 = min(cnt['c'],cnt['d']), b1 = max(cnt['a'],cnt['b']), ...

  2. struts2.1.6教程一、准备工作及实例

    1.解压struts-2.1.6-all.zip apps目录:struts2自带的例子程序 docs目录:官方文档. lib 目录:存放所有jar文件. Src 目录:源文%件存放地 2.六个基本包 ...

  3. JS 一条原型链扯到底

    在正文之前,首先要知道两点, 1.__proto__是每个js 对象的内置属性,而prototype 是函数的内置属性,也是一个对象. 2.所谓原型,指的就是每个函数对象的prototype属性. f ...

  4. Openstack虚拟机在线迁移(Live Migration)

    Openstack VM live migration can have 3 categories: -Block live migration without shared storage -Sha ...

  5. DelayQueue使用

    假设现有如下的使用场景: a) 关闭空闲连接.服务器中,有很多客户端的连接,空闲一段时间之后需要关闭之. b) 缓存.缓存中的对象,超过了空闲时间,需要从缓存中移出. c) 任务超时处理.在网络协议滑 ...

  6. mysql5.6 rpm安装配置

    检查MySQL及相关RPM包,是否安装,如果有安装,则移除(rpm –e 名称)   [root@localhost share]# rpm -qa | grep -i mysql MySQL-cli ...

  7. php笔记一

    一.Login登陆验证步骤: 1.赋值,用$_POST将id和password传递给loginProcess.php登陆验证页面. 2.建立数据库连接 $conn=mysql_connect($hos ...

  8. Hybrid App开发之JavaScript基础

    前言: 前面学习了html和css的基本使用,今天开始学习JavaScript的使用. 什么是JavaScript JavaScript是一种基于对象(Object)和事件驱动(Event Drive ...

  9. 并发容器ConcurrentHashMap#put方法解析

    jdk1.7.0_79 HashMap可以说是每个Java程序员用的最多的数据结构之一了,无处不见它的身影.关于HashMap,通常也能说出它不是线程安全的.这篇文章要提到的是在多线程并发环境下的Ha ...

  10. threading多线程总结

    threading用于提供线程相关的操作,线程是应用程序中工作的最小单元.python当前版本的多线程库没有实现优先级.线程组,线程也不能被停止.暂停.恢复.中断. threading模块提供的类:  ...