NOIP Simulation-Traveler Problem Solving report

Source: Internet
Author: User
Tags gcd

Title: http://www.tsinsen.com/P9143

The problem is that I found out. Want to test can be measured. have been shared out.

Traveler questions

"Problem description"

Lahub is a fan of the traveler who wants to be a real traveler, so he plans to start a trip. Lahub wanted to visit n destinations (all on a straight line). Lahub began his travels at the beginning. The distance between the first destination and the starting point is ai km (AI is a nonnegative integer). There are no two destinations and the same distance from the starting point.

The journey from the I destination to the first J destination is |ai-aj| km. We call the order of visits to n destinations a "trip". Lahub can visit any order he wants to visit, but each destination has and can only be visited once (the order of visits is N).

Lahub writes all possible "travels" on a piece of paper and notes the distance each "trip" is going to take. He is interested in the average value of the sum of all "travel" distances. But he thought the calculation was too dull, so he asked you for help.

"Input Format"

The first line is a positive integer n.

The second row n non-negative integers a1,a2,...., an (1≤ai≤10^7).

"Output Format"

Two integers, the answer is output in the simplest fractional form, the first is the numerator and the second is the denominator.

"Input Sample"

3

2 3 5

"Output Example"

22 3

"Sample Prompt"

There are 6 possible travel examples:

[2, 3, 5]: The distance to travel: |2–0| + |3–2| + |5–3| = 5;

[2, 5, 3]: |2–0| + |5–2| + |3–5| = 7;

[3, 2, 5]: |3–0| + |2–3| + |5–2| = 7;

[3, 5, 2]: |3–0| + |5–3| + |2–5| = 8;

[5, 2, 3]: |5–0| + |2–5| + |3–2| = 9;

[5, 3, 2]: |5–0| + |3–5| + |2–3| = 8.

The answer is 1/6 * (5+7+7+8+9+8) =44/6=22/3

"Data Range"

30% n<=10

50% n<=1000

100% n<=100000

Analysis:

This problem will inevitably make people think of using DFS search down. Make a full arrangement out of it. The position between two points may also be preprocessed for full alignment. But. Do these things. Well. It's still for WA.

And this question further ponder? It makes people think. What if, just one treatment for each edge? Think about the effect of---> each edge (distance between two points) on the final answer. So if we go this way, how many more ways are we going to go next, and how many combinations are there next? Let's say we've chosen AI and Aj, and then there's (n-2)! Scenarios. Then AI and AJ will go this way (n-2)! And this road has a choice of (n-1). Because we can choose when to go again. At this time the number of programmes is more. Yes (n-2)!* (n-1) = = (n-1)!. But we just enumerated the distance between two points, without enumerating the paths starting from the starting point. And the position of the path from the beginning is fixed. Let's say we're from 0 to AI. Then there will be (n-1) after the completion of the program. /-Multiplication principle-/. So every time the situation is enumerated. The multiplicative weights are our final sum.



The key is how we enumerate the entire sum (ABS (A[I]-A[J)); First, we can be sure. We only use calculations from I to J and (I>J) This calculation, the total distance is X2 (walk over, come back).

We can use recursion to achieve the entire calculation. We can find a pattern:

Suppose A1 > A2 > a3 > A4,

With Ai for the end of the Ai-aj and for the Si,

So:

S2 = A1-a2

S3 = a1-a3 + a2-a3 = (a1-a2 + a2-a3) + a2-a3 = S2 + 2 *(a2-a3)

S4 = a1-a4 + a2-a4 + a3-a4 = (a1-a3 + a3-a4) + (A2-a3 + a3-a4) + (A3-A4)

= S3 + 3 * (A3-A4)

We have found that S is recursive.

The numerator is the sum of the results at the end of the calculation. And the denominator is the total scheme number n! After the score. Only one n is left in the denominator. Then we just need to calculate gcd and divide it by GCD.

Release code:

#include <cstdio> #include <algorithm>using namespace std;int line[1000001];int n;long long int gcd (Long long int a,long long int b) {    return a%b==0? B:GCD (b,a%b);} int cmp (int a,int b) {    return a>b;} int main () {    freopen ("tourist.in", "R", stdin);    Freopen ("Tourist.out", "w", stdout);    Long long int sum=0;    scanf ("%d", &n);    for (int i=1;i<=n;++i)    {    scanf ("%d", &line[i]);    Sum+=line[i];    }    Sort (line+1,line+1+n,cmp);    Long long int k=0,ans=0;    for (int i=2;i<=n;++i)    {    k=k+ (i-1) * (Line[i-1]-line[i]);    ans+=k;    }    sum+=ans*2;    Long Long int c=gcd (sum,n);    printf ("%i64d%i64d", sum/c,n/c);    Fclose (stdin);    Fclose (stdout);    return 0;}

NOIP Simulation-Traveler Problem Solving report

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.