Skip to content

Commit b393f3b

Browse files
committed
Add an example for ABC145-C
1 parent 8443283 commit b393f3b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

examples/abc145-c.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// https://atcoder.jp/contests/abc145/tasks/abc145_c
2+
//
3+
// 以下のクレートを使用。
4+
//
5+
// - `itertools`
6+
// - `nalgebra`
7+
// - `proconio`
8+
9+
use itertools::Itertools as _;
10+
use nalgebra::{Point2, Scalar};
11+
use proconio::input;
12+
use proconio::source::{Readable, Source};
13+
14+
use std::convert::Infallible;
15+
use std::io::BufRead;
16+
use std::marker::PhantomData;
17+
18+
fn main() {
19+
// `proconio::input!`。
20+
//
21+
// https://docs.rs/proconio/0.3.6/proconio/macro.input.html
22+
input! {
23+
n: usize,
24+
points: [ReadPoint2<f64>; n],
25+
}
26+
27+
// nC2通りの組み合わせを列挙するのに`<_ as itertools::Itertools>::tuple_combinations`を用いる。
28+
// また各点同士の距離√((x_i - x_j)^2 + (y_i - y_j)^2)を求めるのに`nalgebra::distance`を使う。
29+
//
30+
// https://docs.rs/itertools/0.8/itertools/trait.Itertools.html#method.tuple_combinations
31+
// https://docs.rs/nalgebra/0.19/nalgebra/fn.distance.html
32+
let ans = 2.0 / (n as f64)
33+
* points
34+
.into_iter()
35+
.tuple_combinations()
36+
.map(|(p1, p2)| nalgebra::distance(&p1, &p2))
37+
.sum::<f64>();
38+
println!("{}", ans);
39+
}
40+
41+
// `proconio::source::Readable`を実装することで`Usize1`のようなマーカー型を作ることができる。
42+
//
43+
// https://docs.rs/proconio/0.3.6/proconio/source/trait.Readable.html
44+
45+
struct ReadPoint2<T>(Infallible, PhantomData<fn() -> T>);
46+
47+
impl<N: Readable<Output = N> + Scalar> Readable for ReadPoint2<N> {
48+
type Output = Point2<N>;
49+
50+
fn read<R: BufRead, S: Source<R>>(source: &mut S) -> Point2<N> {
51+
Point2::new(N::read(source), N::read(source))
52+
}
53+
}

test-examples.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ url = "https://atcoder.jp/contests/abc144/tasks/abc144_d"
142142
matching = { FloatOr = { abs = 1e-6, rel = 1e-6 } }
143143
meta = { using = ["libm", "proconio"] }
144144

145+
[examples.abc145-c]
146+
type = "Normal"
147+
name = "ABC145: C - Average Length"
148+
url = "https://atcoder.jp/contests/abc145/tasks/abc145_c"
149+
matching = { FloatOr = { abs = 1e-6, rel = 1e-6 } }
150+
meta = { using = ["itertools", "nalgebra", "proconio"] }
151+
145152
[examples.abc150-d]
146153
type = "Normal"
147154
name = "ABC150: D - Semi Common Multiple"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy