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 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]

题意:给出n条木棍,选择其中一部分拼成三角形,并使得面积最大。

最初的想法设一个四维数组,dp[t][i][j][k],为当前第t个木棍组成的三角形三边的长度分别为i,j,k是否可能存在(可能存在就是可以由边界推得,但不一定满足要求),然后枚举i,j,k确定是否满足题意并更新最大面积。

如果t是逆序枚举,那么第一维可以省略,因为总长已知,知道i,j,那么k也可以算出。所以简化到dp[i][j].可令i>=j.

如果dp[i-a[t]][j]存在或者dp[i][j-a[t]]存在 ,那么dp[i][j]就可能存在,标记为1.

然后遍历dp[][],寻找可能存在的dp[i][j],然后确定是否满足题意并更新答案。

这里为什么要标记之后再遍历,而不是标记之后就直接计算?因为标记之后直接计算可能会出现状态为i或j为0,而且一个状态可能被计算多次。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int MAX=0x3f3f3f3f;
int area(double a,double b,double c) {
    double s=(a+b+c)/2;
    return 100*sqrt(s*(s-a)*(s-b)*(s-c));
}
int a[45], dp[810][810],n,sum=0;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    int tmp = sum/2 ,_max=0;
    dp[0][0]=1;
    for(int i=1;i<=n;i++)
        for(int j=tmp;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=tmp;i>=1;i--)
        for(int j=i;j>=1;j--) if( dp[i][j] ) {
            int k=sum-i-j;
            if( i+k<=j || i+j<=k || j+k<=i ) continue;
            int ans = area( i,j,k );
            if( ans > _max ) _max=ans;
        }
    if( _max != 0 ) printf("%d\n",_max);
    else printf("-1\n");
    return 0;
}



POJ 1948 Triangular Pastures(DP)

时间: 2024-08-19 14:17:19

POJ 1948 Triangular Pastures(DP)的相关文章

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

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

POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了. 本题要求装满的,故此增加个限制就可以了. #include <stdio.h> #include <stdlib.h> #include <string.h> inline int min(int a, int b) { return a < b? a : b; } c

POJ 3086 Triangular Sums

Triangular Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6371   Accepted: 4529 Description The nth Triangular number, T(n) = 1 + - + n, is the sum of the first n integers. It is the number of points in a triangular array with n po

POJ 3280 Cheapest Palindrome DP题解

看到Palindrome的题目,首先想到的应该是中心问题,然后从中心出发,思考如何解决. DP问题一般是从更加小的问题转化到更加大的问题,然后是从地往上 bottom up地计算答案的. 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需改动 else tbl[id][i] = min(tbl[!id][i+1] + cost[cowID[i]-'a'], tbl[!id][i

POJ 2342 (树形DP)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

POJ 3034 Whac-a-Mole(DP)

题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在(x1,y1),那下一秒直线移动到的整数点(x2,y2)与这个点的距离小于等于d,并且当锤子移动(x2,y2)这个点时,所有在两点的直线上的整点数都可以打到.例如(0,0)移动到(0,3).如果(0,1),(0,2)有老鼠出现就会被打到.求能够打的最多老鼠. 思路 : Dp[i][j][k]代表点(i,j)

POJ 3616 Milking Time DP题解

典型的给出区间任务和效益值,然后求最大效益值的任务取法. 属于一维DP了. 一维table记录的数据含义:到当前任务的截止时间前的最大效益值是多少. 注意, 这表示当前任务一定要选择,但是最终结果是不一定选择最后一个任务,故此最后需要遍历找到table数组的最大值,当然计算过程中使用一个数记录最终最大值也是可以的. 状态转移方程就是: tbl[i] = MAX({from tbl[0]->tbl[i-1] }+ weight[i] ),即区间0到i-1加上i的当前效益值. #include <