codeforces 340C Tourist Problem(公式题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Iahub 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 it 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 - 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.
Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
The first line contains integer n (2 ≤ n ≤ 105). Next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 107).
Output two integers — the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
3
2 3 5
22 3
Consider 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 
 = 
 = 
.
给出n个点的坐标,然后从0出发,可以以任何一个点为终点,问走过的路程的长度的期望
这个的公式证明也是挺容易的。。。懒得写了。。。具体看代码吧。。。
//#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI; #define MAXN 100010
ll a[MAXN];
int main()
{
ios::sync_with_stdio(false);
ll n;
while(cin>>n){
for(int i=;i<n;i++)cin>>a[i];
sort(a,a+n);
ll tx=;
ll ty=,s=;
for(ll i=,j=n-;i<n;i++,j--){
tx+=i*a[i];
ty+=j*a[i];
s+=a[i];
}
s+=(tx-ty)*2LL;
ll tmp=__gcd(s,n);
cout<<s/tmp<<" "<<n/tmp<<endl;
} return ;
}
代码君
codeforces 340C Tourist Problem(公式题)的更多相关文章
- codeforces 340C Tourist Problem
		
link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...
 - codeforces 340C Tourist Problem(简单数学题)
		
题意:固定起点是0,给出一个序列表示n个点,所有点都在一条直线上,其中每个元素代表了从起点到这个点所走的距离.已知路过某个点不算到达这个点,则从起点出发,到达所有点的方案有许多种.求所有方案走的总路程 ...
 - CodeForces - 340 C - Tourist Problem
		
先上题目: A - Tourist Problem Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
 - Codeforces Round #198 (Div. 2) C. Tourist Problem
		
C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...
 - Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)
		
C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...
 - C. Tourist Problem 2021.3.29 晚vj拉题 cf 1600 纯数学题
		
拉题链接 https://vjudge.net/contest/430219#overview 原题链接 https://codeforces.com/problemset/problem/340 ...
 - C. Tourist Problem
		
http://codeforces.com/problemset/problem/340/C 赛时没想出赛后却能较快想出深深的教育自己做题一定要静下心来,不要轻易放弃,认真思考,不要浮躁着急,不要太容 ...
 - Codeforces 707C. Pythagorean Triples-推公式的数学题
		
两道C题题解,能推出来公式简直是无敌. http://codeforces.com/problemset/problem/707/C codeforces707C. Pythagorean Tripl ...
 - Codeforces 732F. Tourist Reform (Tarjan缩点)
		
题目链接:http://codeforces.com/problemset/problem/732/F 题意: 给出一个有n个点m条边的无向图,保证联通,现在要求将所有边给定一个方向使其变成有向图,设 ...
 
随机推荐
- Cogs 12 运输问题2 (有上下界网络流)
			
#include <cstdlib> #include <algorithm> #include <cstring> #include <iostream&g ...
 - poj2456 二分逼近寻找正确答案
			
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10078 Accepted: 4988 ...
 - hdu Find a way
			
算法:广搜: Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Le ...
 - css3实现各种渐变效果,比较适合做手机触屏版
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - AngularJS中的控制器示例_2
			
<!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ...
 - Linux里如何设置IP(RED HAT)
			
一共包括以下两步 1:通过setup命令设置IP 保存…… --如果‘Use DHCP'处事[*],则可能无法手动输入IP,子网掩码和网关. 解决办法: 修改脚本/etc/sysconfig/netw ...
 - XJOI网上同步训练DAY3 T1
			
思路:看来我真是思博了,这么简单的题目居然没想到,而且我对复杂度的判定也有点问题.. 首先我们选了一个位置i的b,那一定只对i和以后的位置造成改变,因此我们可以这样看: 我们从前往后选,发现一个位置的 ...
 - Qt实现基于G.729A(G729A)的语音聊天
			
一.G.729协议简介G.729协议是由ITU-T的第15研究小组提出的,并在1996年3月通过的8Kbps的语音编码协议.G.729系列主要有以下几种:G.729—最基本的G.729标准协议,原始版 ...
 - android的原理--为什么我们不需要手动关闭程序
			
内容搜集自网络,有所删改 不用在意剩余内存的大小,其实很多人都是把使用其他系统的习惯带过来来了.android大多应用没有退出的设计其实是有道理的,这和系统对进程的调度机制有关系.如果你知 ...
 - Subsets II 解答
			
Question Given a collection of integers that might contain duplicates, nums, return all possible sub ...