Codeforces Round #198 (Div. 2) 340C
1 second
256 megabytes
standard input
standard output
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 =
=
.
题意:求出所有走的方案的平均花费。。
思路:先找出规律。我们发现,num中每两个组合都会出现(n - 1)!次,而分母是n!次,所以抵消,分母为n,分子为求出每个数字和其他数字(以及0)的差的绝对值之和即可,直接暴力枚举为O(n^2)会超时。所以这样:先把num从小到大排序,然后算出总和,然后用一个now来记录第i个前i个和,然后总和为num[i] * i - now (i的前半部分) + sum - now -num[i] * (n - i) - num[i](后半部分),这样一来时间复杂度只有O(n)。
要注意的是要用longlong。由于当时编译器只有渣渣vc6.0所以就用__int64了。
代码:
#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;
}
Codeforces Round #198 (Div. 2) 340C的更多相关文章
- Codeforces Round #198 (Div. 2)A,B题解
Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...
- [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)
http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...
- Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理
题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- Codeforces Round #198 (Div. 2)
A.The Wall 题意:两个人粉刷墙壁,甲从粉刷标号为x,2x,3x...的小块乙粉刷标号为y,2y,3y...的小块问在某个区间内被重复粉刷的小块的个数. 分析:求出x和y的最小公倍数,然后做一 ...
- Codeforces Round #198 (Div. 1) B,C 动态规划
比赛时,开了大号去做,算了半天发现不会做A,囧.于是跑去看B,发现很水?于是很快敲完了,但是A不会,没敢交.于是去看C,一直找规律啊,后来总算调了出来,看了一下榜,发现还是算了吧,直接去睡觉了.第二天 ...
- Codeforces Round #198 (Div. 2) —— D
昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...
- Codeforces Round #198 (Div. 2) —— C
C题很容易看懂题目,不过两个循环肯定会TLE,所以得用点小聪明: 首先排好序,因为是全排列,乱序和顺序的结果是一样的: 然后呢···· 如果是数列 1 2 3 4 5 元素1 被 2 3 4 5每个减 ...
- Codeforces Round #198 (Div. 2) —— B
B题是一个计算几何的题,虽然以前看过计算几何的ppt,但一直都没有写过: 昨晚比赛的时候本来想写的,但是怕不熟练浪费时间,太可惜了! 其实没必要选出一个最大的矩形: 以矩形的一条对角线为轴,向上或者向 ...
随机推荐
- Head First SQL笔记
看的时候总结了一下,如下: Chapter 1: 创建数据库 CREATE DATABASE database_name; 使用数据库 USE database_name; 创建表 CRATE TAB ...
- Webform之(简单投票)练习
创建数据库: CREATE table DiaoYanTiMu ( Ids int primary key ,--题目代号 Title varchar() not null ,--要调查的题目 Sel ...
- Visual format language
所谓的VFL语言其实就是Visual format language 的缩写,是一种使用代码添加约束的方式,类似于Masonry SDAutolayout的效果,但是操作起来可能要相对简单.一行代码 ...
- Best practice for Invoke other scripts or exe in PowerShell
Recently, I find I used many different type method to invoke other scripts or exe in PowerShell. May ...
- C++ enum 作用域问题和解决方案
C++ 中的枚举类型继承于 C 语言.就像其他从 C 语言继承过来的很多特性一样,C++ 枚举也有缺点,这其中最显著的莫过于作用域问题--在枚举类型中定义的常量,属于定义枚举的作用域,而不属于这个枚举 ...
- Oracle SQL篇(三)Oracle ROWNUM 与TOP N分析
首先我们来看一下ROWNUM: 含义解释: 1.rownum是oracle为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推.这是一个伪列,可以用于限制查询返回的总行数. 2 ...
- JavaEE Tutorials (8) - Java持久化API介绍
8.1实体96 8.1.1实体类的需求97 8.1.2实体类中的持久化字段和属性97 8.1.3实体的主键101 8.1.4实体关系中的多重性103 8.1.5实体关系中的方向103 8.1.6实体中 ...
- JAVA GUI学习 - JMenuBar菜单条、JMenu菜单、JMenuItem菜单项组件学习
public class MenuBarKnow extends JFrame { JMenuBar jMenuBar; JMenu jMenuFile,jMenuEditor,jMenuAbout; ...
- Linux下安装VNC Server
操作系统centos6.5,在其之上安装vnc server,可利用windows上的vnc client远程登录. 1. 安装 yum install tigervnc-server.x86_64 ...
- java学习之即时通信项目实战
项目总结:这次项目主要是根据视频来的,结果跟到一半感觉跟不上,慢慢自己有了自己的想法,决定自己先不看学习视频,自己先试着写. 总结写前面,算是写的第一个项目吧.项目中遇到几点问题,首先Scoket对 ...