Skip to content

Commit 8630eee

Browse files
committed
Add examples for APC001-C
1 parent 2c0ad1f commit 8630eee

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed

examples/apc001-c-naive.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// https://atcoder.jp/contests/apc001/tasks/apc001_c
2+
//
3+
// `std` only
4+
5+
use std::{cmp, io, panic, process};
6+
7+
macro_rules! read(($ty:ty) => ({
8+
let mut input = "".to_owned();
9+
io::stdin().read_line(&mut input).unwrap();
10+
input.trim_end().parse::<$ty>().unwrap()
11+
}));
12+
13+
fn main() {
14+
panic::set_hook(Box::new(|_| {
15+
// 変なクエリを吐いて`RE`させ、`TLE`を回避
16+
println!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
17+
}));
18+
19+
let n = read!(usize);
20+
21+
let query = |i: usize| -> _ {
22+
println!("{}", i);
23+
match &*read!(String) {
24+
"Vacant" => process::exit(0),
25+
"Male" => false,
26+
"Female" => true,
27+
_ => unreachable!(),
28+
}
29+
};
30+
31+
let first = query(0);
32+
let last = query(n - 1);
33+
34+
// Nは小さいので`Vec`を作って`<[_]>::binary_search`を悪用
35+
(1..n)
36+
.collect::<Vec<_>>()
37+
.binary_search_by(|&i| {
38+
let query = query(i);
39+
if (i % 2 == 0) == (query == first) || ((n - i - 1) % 2 == 0) != (query == last) {
40+
cmp::Ordering::Less
41+
} else {
42+
cmp::Ordering::Greater
43+
}
44+
})
45+
.unwrap();
46+
unreachable!();
47+
}

examples/apc001-c-proconio.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// https://atcoder.jp/contests/apc001/tasks/apc001_c
2+
//
3+
// 以下のクレートを使用。
4+
// - `proconio`
5+
6+
use proconio::source::line::LineSource;
7+
use std::{cmp, io, panic, process};
8+
9+
fn main() {
10+
panic::set_hook(Box::new(|_| {
11+
// 変なクエリを吐いて`RE`させ、`TLE`を回避
12+
println!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
13+
}));
14+
15+
// `proconio::source::line::LineSource`。
16+
//
17+
// https://docs.rs/proconio/0.3.6/proconio/source/line/struct.LineSource.html
18+
let stdin = io::stdin();
19+
let mut stdin = LineSource::new(stdin.lock());
20+
macro_rules! input(($($tt:tt)*) => (proconio::input!(from &mut stdin, $($tt)*)));
21+
22+
// `proconio::input!`。
23+
//
24+
// https://docs.rs/proconio/0.3.6/proconio/macro.input.html
25+
input!(n: usize);
26+
27+
let mut query = |i: usize| -> _ {
28+
println!("{}", i);
29+
input!(s: String);
30+
match &*s {
31+
"Vacant" => process::exit(0),
32+
"Male" => false,
33+
"Female" => true,
34+
_ => unreachable!(),
35+
}
36+
};
37+
38+
let first = query(0);
39+
let last = query(n - 1);
40+
41+
// Nは小さいので`Vec`を作って`<[_]>::binary_search`を悪用
42+
(1..n)
43+
.collect::<Vec<_>>()
44+
.binary_search_by(|&i| {
45+
let query = query(i);
46+
if (i % 2 == 0) == (query == first) || ((n - i - 1) % 2 == 0) != (query == last) {
47+
cmp::Ordering::Less
48+
} else {
49+
cmp::Ordering::Greater
50+
}
51+
})
52+
.unwrap();
53+
unreachable!();
54+
}

examples/apc001-c-text-io.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// https://atcoder.jp/contests/apc001/tasks/apc001_c
2+
//
3+
// 以下のクレートを使用。
4+
// - `text_io`
5+
6+
use std::{cmp, panic, process};
7+
use text_io::read;
8+
9+
fn main() {
10+
panic::set_hook(Box::new(|_| {
11+
// 変なクエリを吐いて`RE`させ、`TLE`を回避
12+
println!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
13+
}));
14+
15+
// `text_io::read!`。
16+
// `read!()`は`read!("{}", std::io::stdin().bytes().map(Result::unwrap))`の短縮となる。
17+
//
18+
// https://docs.rs/text_io/0.1.8/text_io/macro.read.html
19+
let n: usize = read!();
20+
21+
let query = |i: usize| -> _ {
22+
println!("{}", i);
23+
let seat: String = read!();
24+
match &*seat {
25+
"Vacant" => process::exit(0),
26+
"Male" => false,
27+
"Female" => true,
28+
_ => unreachable!(),
29+
}
30+
};
31+
32+
let first = query(0);
33+
let last = query(n - 1);
34+
35+
// Nは小さいので`Vec`を作って`<[_]>::binary_search`を悪用
36+
(1..n)
37+
.collect::<Vec<_>>()
38+
.binary_search_by(|&i| {
39+
let query = query(i);
40+
if (i % 2 == 0) == (query == first) || ((n - i - 1) % 2 == 0) != (query == last) {
41+
cmp::Ordering::Less
42+
} else {
43+
cmp::Ordering::Greater
44+
}
45+
})
46+
.unwrap();
47+
unreachable!();
48+
}

examples/apc001-c-whiteread.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// https://atcoder.jp/contests/apc001/tasks/apc001_c
2+
//
3+
// 以下のクレートを使用。
4+
// - `whiteread`
5+
6+
use std::{cmp, panic, process};
7+
use whiteread::Reader;
8+
9+
fn main() {
10+
panic::set_hook(Box::new(|_| {
11+
// 変なクエリを吐いて`RE`させ、`TLE`を回避
12+
println!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
13+
}));
14+
15+
// `whiteread::Reader`
16+
//
17+
// https://docs.rs/whiteread/0.5.0/whiteread/reader/struct.Reader.html
18+
let mut rdr = Reader::from_stdin_naive();
19+
20+
// `whiteread::Reader::line`で行ごとに値を読める。特にこのようなインタラクティブ問題に適している。
21+
//
22+
// https://docs.rs/whiteread/0.5.0/whiteread/reader/struct.Reader.html#method.line
23+
let n = rdr.line::<usize>().unwrap();
24+
25+
let mut query = |i: usize| -> _ {
26+
println!("{}", i);
27+
match &*rdr.line::<String>().unwrap() {
28+
"Vacant" => process::exit(0),
29+
"Male" => false,
30+
"Female" => true,
31+
_ => unreachable!(),
32+
}
33+
};
34+
35+
let first = query(0);
36+
let last = query(n - 1);
37+
38+
// Nは小さいので`Vec`を作って`<[_]>::binary_search`を悪用
39+
(1..n)
40+
.collect::<Vec<_>>()
41+
.binary_search_by(|&i| {
42+
let query = query(i);
43+
if (i % 2 == 0) == (query == first) || ((n - i - 1) % 2 == 0) != (query == last) {
44+
cmp::Ordering::Less
45+
} else {
46+
cmp::Ordering::Greater
47+
}
48+
})
49+
.unwrap();
50+
unreachable!();
51+
}

examples/testers/apc001-c.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from argparse import ArgumentParser
2+
from subprocess import Popen, PIPE
3+
from typing import List, Any
4+
5+
6+
def main() -> None:
7+
parser = ArgumentParser()
8+
parser.add_argument('exe')
9+
exe = parser.parse_args().exe
10+
11+
# Sample
12+
judge(exe, ['Male', 'Female', 'Vacant'])
13+
14+
15+
def judge(exe: str, seats: List[str]) -> None:
16+
with Popen([exe], stdin=PIPE, stdout=PIPE) as proc:
17+
def write(content: Any) -> None:
18+
proc.stdin.write(f'{content}\n'.encode())
19+
proc.stdin.flush()
20+
21+
write(len(seats))
22+
23+
for _ in range(20):
24+
seat = seats[int(proc.stdout.readline().decode())]
25+
write(seat)
26+
if seat == 'Vacant':
27+
break
28+
else:
29+
raise Exception('run out')
30+
31+
32+
if __name__ == '__main__':
33+
main()

test-examples.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,34 @@ url = "https://atcoder.jp/contests/agc026/tasks/agc026_c"
247247
matching = "Words"
248248
meta = { using = ["either", "itertools", "proconio", "rustc-hash"] }
249249

250+
[examples.apc001-c-naive]
251+
type = "Special"
252+
name = "APC001: C - Vacant Seat"
253+
url = "https://atcoder.jp/contests/apc001/tasks/apc001_c"
254+
tester = ["python", "./examples/testers/apc001-c.py", "{bin}"]
255+
meta = { using = [] }
256+
257+
[examples.apc001-c-proconio]
258+
type = "Special"
259+
name = "APC001: C - Vacant Seat"
260+
url = "https://atcoder.jp/contests/apc001/tasks/apc001_c"
261+
tester = ["python", "./examples/testers/apc001-c.py", "{bin}"]
262+
meta = { using = ["proconio"] }
263+
264+
[examples.apc001-c-text-io]
265+
type = "Special"
266+
name = "APC001: C - Vacant Seat"
267+
url = "https://atcoder.jp/contests/apc001/tasks/apc001_c"
268+
tester = ["python", "./examples/testers/apc001-c.py", "{bin}"]
269+
meta = { using = ["text_io"] }
270+
271+
[examples.apc001-c-whiteread]
272+
type = "Special"
273+
name = "APC001: C - Vacant Seat"
274+
url = "https://atcoder.jp/contests/apc001/tasks/apc001_c"
275+
tester = ["python", "./examples/testers/apc001-c.py", "{bin}"]
276+
meta = { using = ["whiteread"] }
277+
250278
[examples.apg4b-a]
251279
type = "Normal"
252280
name = "APG4b: A - 1.00.はじめに"

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