Skip to content

Commit 2c0ad1f

Browse files
committed
Add examples for judge-update-202004-{A..D}
1 parent 44be0d2 commit 2c0ad1f

File tree

5 files changed

+191
-0
lines changed

5 files changed

+191
-0
lines changed

examples/judge-update-202004-a.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://atcoder.jp/contests/judge-update-202004/tasks/judge_update_202004_a
2+
//
3+
// 以下のクレートを使用。
4+
// - `num`
5+
// - `num-traits`
6+
// - `proconio`
7+
8+
use proconio::input;
9+
10+
fn main() {
11+
// `proconio::input!`で入力を読む。
12+
//
13+
// https://docs.rs/proconio/0.3/proconio/macro.input.html
14+
input! {
15+
s: i32,
16+
l: i32,
17+
r: i32,
18+
}
19+
20+
// `num_traits::clamp`で解く。
21+
// 定義は`clamp(s, l, r) == if s < l { l } else if s > r { r } else { s }`。
22+
// `PartialOrd`までしか要求しないので`f64`に対しても使える。
23+
//
24+
// https://docs.rs/num-traits/0.2/num_traits/fn.clamp.html
25+
println!("{}", num::clamp(s, l, r));
26+
}

examples/judge-update-202004-b.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://atcoder.jp/contests/judge-update-202004/tasks/judge_update_202004_b
2+
//
3+
// 以下のクレートを使用。
4+
// - `itertools`
5+
// - `proconio`
6+
7+
use itertools::Itertools as _;
8+
use proconio::{fastout, input};
9+
10+
// `#[proconio::fastout]`で標準出力を高速化する。
11+
//
12+
// https://docs.rs/proconio-derive/0.1/proconio_derive/attr.fastout.html
13+
#[fastout]
14+
fn main() {
15+
// `proconio::input!`で入力を読む。
16+
//
17+
// https://docs.rs/proconio/0.3/proconio/macro.input.html
18+
input! {
19+
xcs: [(u32, char)],
20+
}
21+
22+
// `itertools::Itertools::sorted_by_key`で昇順かつ`R`, `B`の順に並び換えた(色, 書かれた整数)のイテレータを得る。
23+
// その実装は単純に`Vec<_>`にしたあと`.sort_by_key(..)`して`.into_iter()`しているだけ。
24+
//
25+
// https://docs.rs/itertools/0.9/itertools/trait.Itertools.html#method.sorted_by_key
26+
for (x, _) in xcs.into_iter().sorted_by_key(|&(x, c)| (c == 'B', x)) {
27+
println!("{}", x);
28+
}
29+
}

examples/judge-update-202004-c.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// https://atcoder.jp/contests/judge-update-202004/tasks/judge_update_202004_c
2+
//
3+
// 以下のクレートを使用。
4+
// - `itertools`
5+
// - `itertools-num`
6+
// - `proconio`
7+
8+
use itertools::{iproduct, Itertools as _};
9+
use itertools_num::ItertoolsNum as _;
10+
use proconio::input;
11+
use std::iter;
12+
13+
fn main() {
14+
// `proconio::input!`で入力を読む。
15+
//
16+
// https://docs.rs/proconio/0.3/proconio/macro.input.html
17+
input! {
18+
r#as: [usize; 3],
19+
}
20+
21+
// `itertools::Itertools::permutations`で順列を列挙。
22+
// 一つ一つ`Vec<_>`を作るので気になるなら`superslice`/`permutohedron`の`next_permutation`か`permutohedron`の`heap_recursive`を。
23+
//
24+
// https://docs.rs/itertools/0.9/itertools/trait.Itertools.html#method.permutations
25+
// https://docs.rs/superslice/1/superslice/trait.Ext.html#tymethod.next_permutation
26+
// https://docs.rs/permutohedron/0.2/permutohedron/trait.LexicalPermutation.html#tymethod.next_permutation
27+
// https://docs.rs/permutohedron/0.2/permutohedron/fn.heap_recursive.html
28+
let ans = (0..r#as.iter().sum())
29+
.permutations(r#as.iter().sum())
30+
.filter(|perm| {
31+
// `itertools_num::ItertoolsNum::cumsum`で累積和のイテレータが得られる。
32+
// そしてその"windows"を`itertools::Itertools::tuple_windows`でイテレータからそのまま得る。
33+
// 今回場合`perm`を3分割するだけだが..
34+
//
35+
// https://docs.rs/itertools-num/0.1/itertools_num/trait.ItertoolsNum.html#method.cumsum
36+
// https://docs.rs/itertools/0.9/itertools/trait.Itertools.html#method.tuple_combinations
37+
let x = iter::once(&0)
38+
.chain(&r#as)
39+
.cumsum()
40+
.tuple_windows()
41+
.map(|(c1, c2)| &perm[c1..c2])
42+
.collect::<Vec<_>>();
43+
44+
// `for i in 0..3 { for j in 0..3 { .. } }`の代わりに`itertools::iproduct!`を使う。
45+
//
46+
// https://docs.rs/itertools/0.9/itertools/macro.iproduct.html
47+
48+
let horz = iproduct!(0..3, 0..3)
49+
.filter(|&(i, j)| i > 0 && j < r#as[i])
50+
.all(|(i, j)| x[i][j] > x[i - 1][j]);
51+
52+
let vert = iproduct!(0..3, 0..3)
53+
.filter(|&(i, j)| j > 0 && j < r#as[i])
54+
.all(|(i, j)| x[i][j] > x[i][j - 1]);
55+
56+
horz && vert
57+
})
58+
.count();
59+
println!("{}", ans);
60+
}

examples/judge-update-202004-d.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// https://atcoder.jp/contests/judge-update-202004/tasks/judge_update_202004_d
2+
//
3+
// 以下のクレートを使用。
4+
// - `proconio`
5+
// - `superslice`
6+
7+
use proconio::{fastout, input};
8+
use superslice::Ext as _;
9+
10+
// `#[proconio::fastout]`で標準出力を高速化する。
11+
//
12+
// https://docs.rs/proconio-derive/0.1/proconio_derive/attr.fastout.html
13+
#[fastout]
14+
fn main() {
15+
// `proconio::input!`で入力を読む。
16+
//
17+
// https://docs.rs/proconio/0.3/proconio/macro.input.html
18+
input! {
19+
n: usize,
20+
q: usize,
21+
r#as: [usize; n],
22+
ss: [usize; q],
23+
}
24+
25+
// `num_integer::gcd`でGCDを得る。
26+
//
27+
// https://docs.rs/num-integer/0.1/num_integer/fn.gcd.html
28+
let gcds = r#as
29+
.into_iter()
30+
.scan(0, |gcd, a| {
31+
*gcd = num::integer::gcd(*gcd, a);
32+
Some(*gcd)
33+
})
34+
.collect::<Vec<_>>();
35+
36+
for s in ss {
37+
// "j"を`superslice::Ext::lower_bound_by`で二分探索することで求める。
38+
//
39+
// https://docs.rs/superslice/1/superslice/trait.Ext.html#tymethod.lower_bound_by
40+
let j = gcds.lower_bound_by(|&gcd| 1usize.cmp(&num::integer::gcd(gcd, s)));
41+
let ans = if j < n {
42+
j + 1
43+
} else {
44+
num::integer::gcd(s, gcds[n - 1])
45+
};
46+
println!("{}", ans);
47+
}
48+
}

test-examples.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,34 @@ url = "https://atcoder.jp/contests/atc002/tasks/atc002_b"
290290
matching = "Words"
291291
meta = { using = ["num", "proconio"] }
292292

293+
[examples.judge-update-202004-a]
294+
type = "Normal"
295+
name = "Judge System Update Test Contest 202004: A - Walking Takahashi"
296+
url = "https://atcoder.jp/contests/judge-update-202004/tasks/judge_update_202004_a"
297+
matching = "Words"
298+
meta = { using = ["num", "proconio"] }
299+
300+
[examples.judge-update-202004-b]
301+
type = "Normal"
302+
name = "Judge System Update Test Contest 202004: B - Picking Balls"
303+
url = "https://atcoder.jp/contests/judge-update-202004/tasks/judge_update_202004_b"
304+
matching = "Words"
305+
meta = { using = ["itertools", "proconio"] }
306+
307+
[examples.judge-update-202004-c]
308+
type = "Normal"
309+
name = "Judge System Update Test Contest 202004: C - Numbering Blocks"
310+
url = "https://atcoder.jp/contests/judge-update-202004/tasks/judge_update_202004_c"
311+
matching = "Words"
312+
meta = { using = ["itertools", "itertools-num", "proconio"] }
313+
314+
[examples.judge-update-202004-d]
315+
type = "Normal"
316+
name = "Judge System Update Test Contest 202004: D - Calculating GCD"
317+
url = "https://atcoder.jp/contests/judge-update-202004/tasks/judge_update_202004_d"
318+
matching = "Words"
319+
meta = { using = ["proconio", "superslice"] }
320+
293321
[examples.panasonic2020-d]
294322
type = "Normal"
295323
name = "Panasonic Programming Contest 2020: D - String Equivalence"

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