HDU 5135 Little Zu Chongzhi ' s triangles (greedy)

Source: Internet
Author: User

Little Zu Chongzhi ' s trianglesTime limit:2000/1000 MS (java/others) Memory limit:512000/512000 K (java/others)
Total submission (s): 304 Accepted Submission (s): 180


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)


Main topic:

Given the length of some line segments, they are required to be combined into some triangles, making the total area the largest.


Problem Solving Ideas:

According to Helen Formula, can be greedy solution. That is, the larger the size of the triangle, the larger the area.

Therefore, after sorting all the edges, from large to small, if the largest three edges can form a triangle, then use this combination;

If a triangle cannot be formed, that is, the second and third largest Benzi and not larger than the first (because it is sorted, other restrictions are not considered), the first large edge is discarded because any combination of subsequent edges cannot be a triangle with the largest edge.


Reference code:

#include <algorithm> #include <iostream> #include <cstdio> #include <cmath>using namespace std; const int MAXN = 20;double ans;int N, l[maxn];void init () {    ans = 0;} void input () {    for (int i = 0; i < n; i++) {        scanf ("%d", &l[i]);}    } Double CalcArea (int i) {    double A = l[i], B = l[i-1], c = l[i-2];    Double p = (A + B + C)/2.0;    return sqrt (P * (p-a) * (p-b) * (P-C));} void Solve () {    sort (l, l+n);    int i = n-1;    while (I >= 2) {        if (L[i] <= l[i-1] + l[i-2]) {            ans + = CalcArea (i);            i = i-3;        } else {            i = i-1;        }    }    printf ("%.2lf\n", ans);} int main () {    while (~scanf ("%d", &n) && N) {        init ();        Input ();        Solve ();    }    return 0;}


HDU 5135 Little Zu Chongzhi ' s triangles (greedy)

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.