https://atcoder.jp/contests/abc268/tasks/abc268_b
剰余の頻度を集計していますが、めんどうですね。
// Chinese Restaurant #![allow(non_snake_case)] fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } fn modulus(n: i32, d: i32) -> i32 { let r = n % d; if r >= 0 { r } else { r + d } } fn max_num_persons(p: &Vec<i32>) -> i32 { let N: usize = p.len(); let M = N as i32; let mut rs: Vec<i32> = (0..N).map(|_| 0).collect(); for i in 0..N { rs[modulus(p[i] - (i as i32), M) as usize] += 1 } (0..N).map(|i| rs[i] + rs[(i+1)%N] + rs[(i+2)%N]).max().unwrap() } fn main() { let _N: usize = read(); let p = read_vec(); println!("{}", max_num_persons(&p)) }