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

    Source
    USACO 2002 February

    分析:
    我们可以通过两条边得出第三条边的长度
    那么我们只用看看两条边怎么分配就好了
    这就有点像一个背包,但是只判断可行性
    还是枚举每一条line以及组成的两条边的长度
    f[j][k] 一条边是j,一条边是k,那么剩下的一条边就是sum-k-j
    f[j][k]=f[j][k]||f[j-a[i]][k]
    f[j][k]=f[j][k]||f[j][k-a[i]]

    三边求出来之后
    我们要怎么计算三角形面积呢:
    这里写图片描述

    这里写代码片
    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    
    int ans=-1;
    int n,sum=0;
    int a[100];
    bool f[2000][2000];
    
    void js(int x,int y)
    {
        int z=sum-x-y;
        if (x+y>z&&y+z>x&&x+z>y)
        {
            double S=(double)(x+y+z)*(x+y-z)*(x+z-y)*(y+z-x);
            S=sqrt(S); S=S/4;
            S=S*100;
            ans=max(ans,(int)S);
        }
    }
    
    int main()
    {
        scanf("%d",&n);
        for (int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
        int i,j,k;
        f[0][0]=1;
        for (i=1;i<=n;i++)
            for (j=sum;j>=0;j--)
                for (k=sum-j;k>=0;k--)
                {
                    if (j>=a[i]) f[j][k]=f[j][k]||f[j-a[i]][k];
                    if (k>=a[i]) f[j][k]=f[j][k]||f[j][k-a[i]];
                    if (f[j][k]) js(j,k);
                }
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    java删除文件夹及子目录
    SQL语法精讲(包括建库、建表、建视图、查询、增加、删除、)
    图片压缩工具类
    数据库中的java.sql.Timestamp转换成Date
    mysql数据库连接工具类C3P0
    java发送邮件带附件
    java下载文件
    web程序定时器
    数据库之函数、数据类型转换(转)
    数据库基础(变量、运算符、if语句、while语句)
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673200.html
Copyright © 2020-2023  润新知