Triangular Pastures POJ - 1948

Triangular Pastures POJ - 1948

sum表示木条的总长。a[i]表示第i根木条长度。ans[i][j][k]表示用前i条木条,摆成两条长度分别为j和k的边是否可能。

那么ans[i][j][k]=ans[i-1][j-a[i]][k] || ans[i-1][j][k-a[i]]

可以用滚动数组优化。

最后在ans[n]中枚举i和j,如果ans[n][i][j]为true,再算出sum-i-j的长度,判断是否存在分别以i,j,sum-i-j为三边长的三角形。如果存在,再用海伦公式算出三角形面积,用面积去更新最大面积。

错误记录:
没有在dp的时候判边界,也就是没有判j>=a[i]和k>=a[i]。

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 bool ans[1601][1601];
 7 LL a[41],n,sum;
 8 double p,anss;
 9 int main()
10 {
11     LL i,j,k,x,y,z;
12     scanf("%lld",&n);
13     for(i=1;i<=n;i++)
14     {
15         scanf("%lld",&a[i]);
16         sum+=a[i];
17     }
18     ans[0][0]=1;
19     for(i=1;i<=n;i++)
20         for(j=sum;j>=0;j--)
21             for(k=sum;k>=0;k--)
22             {
23                 if(j>=a[i])
24                     ans[j][k]|=ans[j-a[i]][k];
25                 if(k>=a[i])
26                     ans[j][k]|=ans[j][k-a[i]];
27             }
28     for(x=1;x<=sum;x++)
29         for(y=x;y<=sum-x-1;y++)
30         {
31             if(!ans[x][y])    continue;
32             z=sum-x-y;
33             if(y>z)    break;
34             if(x+y<=z) continue;
35             p=(double)(x+y+z)/2.0;
36             anss=max(anss,sqrt(p*(p-x)*(p-y)*(p-z)));
37         }
38     if(anss!=0.0)
39         printf("%lld",(long long)(anss*(double)100));
40     else
41         printf("-1");
42     return 0;
43 }
时间: 2024-08-24 11:38:51

Triangular Pastures POJ - 1948的相关文章

POJ 1948 Triangular Pastures(DP)

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 triangu

POJ 1948 Triangular Pastures(二维背包)

Triangular Pastures Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6774   Accepted: 2199 Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geo

(DP) poj 1948

Triangular Pastures Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6783   Accepted: 2201 Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geo

Triangular Pastures

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 triangu

POJ 1948

这道题我记得是携程比赛上的一道. 开始时想直接设面积,但发现不可以,改设能否构成三角形.设dp[i][j][k]为前i根木棍构成边长为j和k的三角形,那么转移可以为dp[i][j][k]=dp[i-1][j-len[i]][k]|dp[i-1][j][k-len[i]].当发现可以构成三角形时,再用海伦公式求出三角形面积即可. 由于边长不应该超过总长一半,所以,DP范围只在一半即可. #include <iostream> #include <cstdio> #include &l

POJ - 1948 二维01背包

T了两发,DP方程很简单粗暴 dp[i][j][k]:用前i物品使得容量分别为j和k的背包恰好装满 背包的调用只需一次即可,第一次T就是每次check都丧心病狂地背包一次 对于sum的枚举,其实i j枚举到sum/2就可以了 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #define scan(a) scanf(

poj 01背包

首先我是按这篇文章来确定题目的. poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<algorithm> #include<cmath> #include<iostream> #include<cstring> using namespace std; int w[3403]; int h[3403]; int n,m; int dp[12880+9]; i

POJ之01背包系列

poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using namespace std; #define N 15010 int n,m,v[N],c[N],f[N]; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d%d",&am

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY