ABC145 C - Average Length
Table of contents
# Problem
https://atcoder.jp/contests/abc145/tasks/abc145_c
Given integer N and N points , report the average of the sum of lengths of all permutations of points.
# Explanation
For generating permutations of an array, you can use next_permutation
function in algorithm
library.
See the solution below for detailed usage.
# Time complexity
$O(N \cdot N!)$.
# Solution
#define MAX_N 8
Int N;
vector<Vector2> C(MAX_N);
void input() {
cin >> N;
loop(n,0,N) cin >> C[n];
C.resize(N);
}
double distance() {
double d = 0;
loop(n,0,N-1) {
d += (C[n+1] - C[n]).length();
}
return d;
}
void solve() {
sort(span_all(C));
double d = 0;
Int p = 0;
do {
p++;
d += distance();
} while (next_permutation(span_all(C)));
cout << d / p << endl;
}
int main(void) {
cout.precision(15);
input();
solve();
return 0;
}
Shun
Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!