Poj 2231 Moo volume (sorting + simple formula derivation)

Source: Internet
Author: User

// Mathematical formula derivation <br/> // sort first, it is easy to deduce and write <br/> // A1 A2 A3 A4 A5 <br/> // 1 2 3 4 5 <br/> // D1 D2 D3 D4 D5 (meaning very apparently) <br/> // 0 1 2 3 4 <br/> // 1 0 1 2 3 <br/> // 2 1 0 1 2 <br/>/3 2 1 0 1 <br/> // 4 3 2 1 0 <br/> // It is a symmetric matrix, adding all the values together is the final answer <br/> // further induction brings the points of the matrix into these relations, such as A3-A2, and adds them all together to get the formula: <br/> // ans = 4 * (A5-A1) + 3 * (A4-A2) + 2 * (A3-A3) + 1 * (A2-A4) <br/> # include <iostream> <br/> # include <algorithm> <br/> using namespace STD; <br/> int Main () <br/>{< br/> long int ans = 0, arr [10010]; // It takes me a long int to survive, and it has affected my wa for a long time, previously, ANS used long int instead of arr. An error may occur when adding different types ...... N times more than wa! Lessons! <Br/> int n, m; <br/> CIN> N; <br/> for (INT I = 0; I <n; ++ I) <br/> {<br/> CIN> arr [I]; // It is faster to use scanf, but you do not know how to read the long int format ...... Which of the following experts will you advise ...... <Br/>}< br/> sort (ARR, arr + n); </P> <p> for (INT I = 0; I <n; ++ I) <br/> {<br/> ans + = (n-1-i) * (ARR [n-1-i]-Arr [I]); <br/>}< br/> cout <ans * 2 <Endl; <br/> return 0; <br/>} 

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.