Triangular Pastures
Total Submission(s) : 94 Accepted Submission(s) : 33
Problem 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.
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.
* 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
package DynamicProgrammingPackage; import java.util.Arrays; import java.util.Scanner; /* * 二维0-1背包问题 * 问题描述:给你一些边求这些栅栏,然后用这些栅栏构造一个三角形(栅栏不能截断), * 求能组成的最大三角形的面积 * 思路: * 状态:v[i][j]==true代表根据这些栅栏能够构造出长度为i和j的栅栏 * 转移方程:v[i][j]=v[i-h[x]][j] || v[i][j-h[x]] * 因为如果能v[i-h[x]][j]或者v[i][j-h[x]]如果为true, * 则加上长度为h[x]的栅栏之后,就能够造出v[i][j] * 如何变成:用倒序的方式压缩空间 */ public class TriangularPastures{ //每个最大长度(40)*最大片段数(40)=三角形的最大面积(1600),所以三角形的最大边长是800 public static final int N = 801; //背包的第一维和第二维分别是三角形的两条边 public static boolean[][] v = new boolean[N][N];//背包容量 public static int m;//一共有多少个栅栏 public static int L;//三角形的周长 public static int half;//三角形周长的一半 public static int h[] = new int[40]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNextInt()) { m = sc.nextInt(); L = 0; for(int i=0; i<m; i++) { h[i] = sc.nextInt(); L += h[i]; } half = L >> 1; fillBack(half+1); dp(); double result = maxArea(); if(result < 0) { System.out.println("-1"); } else { System.out.printf("%d\n", (int)(Math.sqrt(result)*100)); } } } private static double maxArea() { double maxArea = -1; double tempArea = 0; //开始的时候用的是L >> 1,一直Wrong answer double half1 = L / 2.0; int a = 0; for(int i=1; i<half; i++) { for(int j=1; j<half; j++) { if(v[i][j] == true) { a = L - i - j; if(i+j>a && i+a>j && j+a>i) {//判断两边之和大于第三边 //三角形的面积=√L/2*(L/2-a)*(L/2-b)*(L/2-c),这里没有开根号,等输出的时候再开根号 tempArea = half1 * (half1-a) * (half1-i) * (half1-j); maxArea = Math.max(tempArea, maxArea); } } } } return maxArea; } /** * 二维0-1背包 */ private static void dp() { v[0][0] = true; for(int i=0; i<m; i++) { for(int j=half; j>=0; j--) {//为了压缩空间,所以倒着求 for(int k=j; k>=0; k--) { //不用判断(j>=h[i] && v[j-h[i]][k]==true),因为v[j-h[i]][k]原本就是false if(j>=h[i]) { v[j][k] = v[j-h[i]][k]; } if(k>=h[i]) { v[j][k] = v[j][k-h[i]]; } } } } } /** * 初始化数组 * @param max 输入的栅栏数目 */ private static void fillBack(int max) { for(int i=0; i<max; i++) { Arrays.fill(v[i], false); } } }