现在的位置: 首页 > 综合 > 正文

poj 1948 Triangular Pastures 小结

2014年07月06日 ⁄ 综合 ⁄ 共 1762字 ⁄ 字号 评论关闭

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;
}

抱歉!评论已关闭.