2012-06-21 15:46:14Morris

[UVA] 11579 - Triangle Trouble

Problem C: Triangle Trouble

These are also limited edition triangles

There is trouble at the triangle factory. The triangle assembler has gone down, so all that has been produced for the day is a bunch of triangle sides. To make the best of this situation, it has been decided to create the triangle with the largest possible area from the available sides, and sell it as a limited edition triangle.

You have been hired to write a program that will determine the area of the limited edition triangle.

Input begins with the number of test cases on its own line. Each test case begins with a positive integer N (3 ≤ N ≤ 10,000), followed by N positive real numbers si representing the lengths of the available triangle sides (0 < si ≤ 100,000). A single test case may be spread out over several consecutive lines of the input.

For each test case, output a line containing the largest possible area of a triangle built using three of the given sides (as a real number rounded to 2 decimal places). If it is not possible to construct any triangles then output "0.00" (quotes for clarity).

Sample input

2
4 3.0 4.0 5.0 100.0
3 1.0 2.0 4.0

Sample output

6.00
0.00


重新寫過了, 優化了許多部份,
答案的部分仍然是將邊做排序, 找連續三個可以湊成三角形的邊構成的最大三角形

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
inline double ReadPoint() {
static char c;
int p = 0;
double t = 1;
while((c = getchar()) >= '0')
t /= 10, p = (p << 3) + (p << 1) + (c-'0');
return (double)p*t;
}
inline int ReadDouble(double *x) {
static char c, neg;
while((c = getchar()) < '-') {if(c == EOF) return EOF;}
if(c == '.') {*x = ReadPoint();return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = getchar()) >= '0')
*x = (*x)*10 + c-'0';
if(c == '.') *x += ReadPoint();
*x *= neg;
return 0;
}
int main() {
int t, n, i;
double A[10000];
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
for(i = 0; i < n; i++) {
ReadDouble(A+i);
}
sort(A, A+n);
double area = 0.0, tmp, s;
for(i = 2; i < n; i++) {
if(A[i-2]+A[i-1] <= A[i])
continue;
s = (A[i-2]+A[i-1]+A[i])/2;
tmp = s*(s-A[i-2])*(s-A[i-1])*(s-A[i]);
if(area < tmp)
area = tmp;
}
area = sqrt(area);
printf("%.2lf\n", area);
}
return 0;
}