Codeforces Round #198 (Div. 2) 340C

Source: Internet
Author: User

C. tourist Problemtime limit per test 1 secondmemory limit per test 256 megabytesinput standard inputoutput standard outputIahub is a big fan of tourists. he wants to become a tourist himself, so he planned a trip. there are n destinations on a straight road that Iahub wants to visit. iahub starts the excursion from kilometer 0. the n destinations are described by a non-negative integers sequencea1, A2 ,..., an. the number ak represents that the kth destination is at distance ak kilometers from the starting point. no two destinations are located in the same place. iahub wants to visit each destination only once. note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit at that point. also, after Iahub visits his last destination, he doesn 'T come back to kilometer 0, as he stops his trip at the last destination. the distance between destination located at kilometer x and next destination, located at kilometer y, is | x region-same y | kilometers. we call a "route" an order of visiting the destinations. iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. ia Hub starts writing out on a paper all possible routes and for each of them, he notes the total distance he wocould walk. he's interested in the average number of kilometers he wocould walk by choosing a route. as he got bored of writing out all the routes, he asks you to help him. inputThe first line contains integer n (2 records ≤ limit n records ≤ limit 105 ). next line contains n distinct integers a1, a2 ,..., an (1 Gbit/s ≤ artificial ai Gbit/S ≤ artificial intelligence 107). OutputOutput two integers-the numerator and denominator of a fraction which is equal to the wanted average number. the fraction must be irreducible. sample test (s) input32 3 5output22 3 NoteConsider 6 possible routes: [2, 3, 5]: total distance traveled: | 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 average travel distance is =. question: calculate the average cost of all solutions .. Idea: first find out the rule. We found that every two combinations of num will appear (n-1 )! Times, while the denominator is n! So offset, the denominator is n, the numerator is to find the sum of the absolute values of the difference between each number and other numbers (and 0), and the direct brute force enumeration is O (n ^ 2) timeout. So this way: first sort num from small to large, then calculate the sum, and then use a now to record the first I sum, then the sum is num [I] * I-now (first half of I) + sum-now-num [I] * (n-I) -num [I] (the second half), so that the time complexity is only O (n ). Note that longlong is used. Since the compiler only had slag vc6.0 at that time, _ int64 was used. Code:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int main() {__int64 sum = 0, now = 0, ans = 0, num[100005], n, i;num[0] = 0;scanf("%I64d", &n);for (i = 1; i <= n; i ++) {scanf("%I64d", &num[i]);sum += num[i];}sort(num, num + 1 + n);for (i = 1; i <= n; i ++) {ans += 2 * num[i] * i - 2 * now + sum - num[i] * (n + 1);now += num[i];}__int64 a, b;a = ans; b = n;if (a < b) {__int64 t = b;b = a;a = t;}while (b) {__int64 sb = b;b = a % b;a = sb; }printf("%I64d %d\n", ans / a, n / a);return 0;}

 


Related Article

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.