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 regular ect, 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
511334
Sample output
692
Hint
[Which is 100x the area of an equilateral triangle with side length 4]
Question: give n wooden sticks, select a part of them into a triangle, and maximize the area.
The original idea was to set up a four-dimensional array, DP [T] [I] [J] [K]. The three sides of the triangle consisting of the current T-stick are respectively I, j, K is possible (it may exist because it can be pushed by the boundary, but not necessarily meet the requirements), and then I, j, and K are enumerated to determine whether the meaning is satisfied and the maximum area is updated.
If t is an inverted enumeration, the first dimension can be omitted. Because the total length is known and I, j is known, K can also be calculated. Therefore, it is simplified to DP [I] [J]. I> = J.
If DP [I-A [T] [J] exists or DP [I] [J-A [T] exists, then DP [I] [J] may exist, marked as 1.
Traverse DP [] [] to find the possible DP [I] [J], determine whether the question is satisfied, and update the answer.
Here, why do we need to mark and traverse it, instead of directly calculating it after marking it? Because it is calculated directly after marking, the status may be I or J is 0, and a status may be calculated multiple times.
#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;}
Zookeeper