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 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条线段构成的三角形的最大面积*100

  题解

  由于三角形有三个边,并且所有的线段都要用到,于是可以用dp[i][j]表示边长为i,j,sum-i-j的三角形,由于是二维的,j,k边有一个可以取到0,另一个减去a后要大于等于0,最后用海伦公式算面积。

  代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[1005][1005];
int n,sum;
int main()
{
    while(~scanf("%d",&n))
    {
        sum=0;
        int a;
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            sum+=a;
            for(int j=1000;j>=0;j--)
            {
                for(int k=1000;k>=0;k--)
                {
                    if((k-a>=0&&dp[j][k-a])||(j-a>=0&&dp[j-a][k]))
                    {
                        dp[j][k]=1;
                    }
                }
            }
        }
        int ans=0;
        for(int i=1;i<=sum/2;i++)
        {
            for(int j=1;j<=sum/2;j++)
            {
               if(dp[i][j]&&(i+j>sum-i-j))
               {
                   double p=sum*1.0/2;
                   int temp=(int)(sqrt(p*(p-i)*(p-j)*(p-sum+i+j))*100);
                   ans=max(ans,temp);
               }
            }
        }
        if(ans)
        printf("%d\n",ans);
        else
        printf("-1\n");
    }
    return 0;
}
时间: 2024-10-16 01:40:48

POJ 1948 Triangular Pastures(二维背包)的相关文章

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 1984 Navigation Nightmare 二维带权并查集

题目来源:POJ 1984 Navigation Nightmare 题意:给你一颗树 k次询问 求2点之间的曼哈顿距离 并且要在只有开始k条边的情况下 思路:按照方向 我是以左上角为根 左上角为原点 dx[i]为i点距离根的x坐标 dy[]是y坐标 这两个可以通过路径压缩求出 只不过是二维而已 #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int m

二维背包初步

问题 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有一个可付出的最大值(背包容量). 问怎样选择物品可以得到最大的价值. 设这两种代价分别为代价1和代价2,第i件物品所需的两种代价分别为a[i]和b[i].两种代价可付出的最大值(两种背包容量)分别为V和U.物品的价值为c[i]. 算法 费用加了一维,只需状态也加一维即可. 设f[i][v][u]表示前i件物品付出两种代价分别为v和u时可获得的最大价值. 状态转移方程就是:f [i]

B 二维背包+完全背包 Hdu2159

<span style="color:#3333ff;">/* ------------------------------------------------------------------------------------------------ author : Grant Yuan time : 2014.7.19 aldorithm: 二维背包+完全背包 ----------------------------------------------------

hdu2159 FATE 二维背包

感觉用背包去想反而麻烦..就是一个递推 #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <map> #define inf 0x3f3f3f3f #define ll __int64 us

二维背包(钟神想要的)(不是DP)

[问题描述] 背包是个好东西,希望我也有.给你一个二维的背包,它的体积是? × ?.现在你有一些大小为1× 2和1×3的物品,每个物品有自己的价值.你希望往背包里面装一些物品,使得它们的价值和最大,问最大的价值和是多少. [输入格式] 第一行一个整数?代表该测试点的数据组数.对于每组数据,第一行有四个整数?,?,? 1 ,? 2 ,其中? 1 ,? 2 分别代表大小为1× 2和大小为1 × 3的物品个数.1 × 2 接下来一行有? 2 个数代表每个1 × 3物品的价值. [输出格式] 对于每组询

POJ 2155 Matrix 【二维树状数组】

题目链接:http://poj.org/problem?id=2155 题目大意:给出一个N*N的0矩阵,下面给出两种指令:1. 给出的第一个数据为'C',再给出四个整形数据,x1,y1,y1,y2,对以(x1,y1)(x2,y2)分别为左上角和右下角坐标的矩阵内的元素进行反转(0变1,1变0)         2. 给出的第一个数据为'Q',再给出两个数据,x,y,然后输出此时这个坐标上的元素. 这题用二维树状数组解,树状数组能够对某区间更新所有元素的和,树状数组维护的是c[1][1]到c[i

HDU 2159FATE(二维背包)

题目地址:HDU 2159 二维的背包,刚开始用的一维,老感觉哪里不对,有两个制约因素.于是就改成了二维的,就过了.. 代码如下: #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <sta

hdu 2660 Accepted Necklace (二维背包)

Accepted Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2530    Accepted Submission(s): 999 Problem Description I have N precious stones, and plan to use K of them to make a necklace f