(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 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.

Sample Input

5
1
1
3
3
4

Sample Output

692

Hint

[which is 100x the area of an equilateral triangle with side length 4]

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n,a[50],dp[810][810],sum,p,ans=-1;
int main()
{
      memset(dp,0,sizeof(dp));
      scanf("%d",&n);
      dp[0][0]=1;
      for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),sum+=a[i];
      for(int i=1;i<=n;i++)
      {
           for(int j=sum/2;j>=0;j--)
           {
                 for(int k=j;k>=0;k--)
                 {
                       if((j>=a[i]&&dp[j-a[i]][k])||(k>=a[i]||dp[j][k-a[i]]))
                       {
                             dp[j][k]=1;
                       }
                 }
           }
      }
      for(int i=sum/2;i>=1;i--)
      {
            for(int j=i;j>=1;j--)
            {
                  if(dp[i][j])
                  {
                        int k=sum-i-j;
                        double p=sum*1.0/2;
                        if(i+j>k&&i+k>j&k+i>j)
                        {
                              int temp=(int)(sqrt(p*(p-i)*(p-j)*(p-k))*100);
                              if(temp>ans)
                                    ans=temp;
                        }
                  }
            }
      }
      printf("%d\n",ans);
      return 0;
}

  

时间: 2024-10-06 21:17:44

(DP) 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

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] poj 1015 Jury Compromise

题目链接: http://poj.org/problem?id=1015 Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24438   Accepted: 6352   Special Judge Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury

[kuangbin 基础dp][POJ 1015] Jury Compromise(dp)

[kuangbin 基础dp][POJ 1015] Jury Compromise 题目 In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is do

(状态压缩DP) poj 2441

Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 3709   Accepted: 1422 Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because the

树形dp poj 2342

题目链接:poj 2342 题目大意:某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大. 思路:既然是树形dp,自然先建立一颗树了,这里用的是邻接表(L<-K).找根的时候利用flag数组来标记所有儿子节点,那么没有标记的自然是根节点了.树形dp从叶子节点开始dp,所以深搜到叶子节点,然后不断回溯给父节点.在处理儿子节点时,父节点不参加

递推DP POJ 1163 The Triangle

题目传送门 1 /* 2 数塔 3 自底向上 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 const int MAXN = 100 + 10; 14 const

状态压缩DP——POJ 2923

对应POJ题目:点击打开链接 Exponentiation Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2923 Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortu

(拓扑图+DP) poj 3249

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9563   Accepted: 2201 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job