[ACM] HDU 5135 Little Zu Chongzhi ' s triangles (a pile of sticks that make up the largest area of the triangle)

Source: Internet
Author: User

Little Zu Chongzhi ' s triangles

Problem Descriptionzu Chongzhi (429–500) was a prominent Chinese mathematician and astronomer during the Liu Song and Sout Hern Qi dynasties. Zu calculated the value ofπto the precision of six decimal places and for a thousand years thereafter no subsequent mathe Matician computed a value this precise. Zu calculated one year as 365.24281481 days, which are very close to 365.24219878 days as we know today. He also worked on deducing the formula for the volume of a sphere.

It's said in some legend stories books that's when Zu were a little boy, he liked mathematical games. One day, his father gave him some wood sticks as toys. Zu Chongzhi found a interesting problem using them. He wanted to make some triangles by those sticks, and he wanted the total area of all triangles he made to be as large as Possible. The rules were:

1) A triangle could only consist of 3 sticks.
2) A triangle ' s vertexes must be end points of sticks. A triangle ' s vertex couldn ' t is in the middle of a stick.
3) Zu didn ' t has to use all sticks.

Unfortunately, Zu didn ' t solve that problem because it is an algorithm problem rather than a mathematical problem. You can ' t solve that problem without a computer if there is too many sticks. So, bring your computer and go back to Zu's time to help him so, maybe you can change the history.
Inputthere is no more than test cases. For each case:

The first line was an integer N (3 <= n<=), indicating the number of sticks Zu Chongzhi had got. The second line contains n integers, meaning the length of n sticks. The length of a stick is no more than 100. The input ends with N = 0.
Outputfor each test case, output the maximum total area of triangles Zu could make. Round the result to 2 digits after decimal point. If Zu couldn ' t make any triangle, print 0.00.
Sample Input
31 1 2073 4 5 3 4 5 900

Sample Output
0.0013.64

SOURCE2014ACM/ICPC Asia Guangzhou Station-re-match (thanks to Chinese laborers and PKU)


Problem Solving Ideas:

Test instructions for a given n sticks, the length of each stick is len[i], to use the N sticks to form some triangles, make the triangle area of the largest, to find the largest area, pay attention to the composition of each triangle can only use three sticks.

See on-Line O (n) practice, is to first n sticks from small to large sequence, and then from the back to scan forward, to see the continuous three sticks can form a triangle, if possible, then the answer plus the area, if not, the biggest stick abandoned, Because the stick can no longer be a triangle with the other two sticks (because it is ordered, the other two root and must be less than the root), and then scan forward .... Finally, the answer is the final answer.

If the difference between the three edges of the triangle is smaller, the larger the area, such as the equilateral triangle area is the largest ... However, the correctness of the practice of the subject is not strictly proved and the guidance is obtained.

Code:

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < algorithm> #include <vector> #include <queue> #include <stack> #include <map> #include < set> #include <cmath> #include <time.h> #include <iomanip> #include <cctype>using namespace STD; #define LL long longbool ok (int a,int b,int c) {return (a+b>c&&a+c>b&&b+c>a);}    Double area (int a,int b,int c) {double p= (a+b+c) *0.5; return sqrt (p* (p-a) * (p-b) * (p-c));            int Len[14];int N;int Main () {while (scanf ("%d", &n)!=eof&&n) {for (int i=1;i<=n;i++)        scanf ("%d", &len[i]);        Sort (len+1,len+1+n);        Double ans=0;        for (int i=n;i>=3;)                {if (ok (len[i],len[i-1],len[i-2])) {Ans+=area (len[i],len[i-1],len[i-2]);            i-=3;        } else i--;    } printf ("%.2lf\n", ans);} return 0;} 



[ACM] HDU 5135 Little Zu Chongzhi ' s triangles (a pile of sticks that make up the largest area of the triangle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.