https://atcoder.jp/contests/abc338/tasks/abc338_e
A < Bとして、Aが小さい弧から見ていって、Aが前のBを超えない限り、Bをスタックしていきます。
// Chord #![allow(non_snake_case)] use std::cmp::{min, max}; //////////////////// library //////////////////// 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 YesNo(b: bool) -> String { return if b { "Yes" } else { "No" }.to_string() } //////////////////// process //////////////////// type Chord = (usize, usize); fn read_chord() -> Chord { let v: Vec<usize> = read_vec(); (v[0], v[1]) } fn read_input() -> Vec<Chord> { let N: usize = read(); let chords: Vec<Chord> = (0..N).map(|_| read_chord()).collect(); chords } fn sort_chords(chords_: Vec<Chord>) -> Vec<Chord> { let mut chords: Vec<Chord> = chords_.into_iter(). map(|(A, B)| (min(A, B), max(A, B))). collect(); chords.sort_by_key(|&(A, _)| A); chords } fn F(chords_: Vec<Chord>) -> bool { let N = chords_.len(); let chords = sort_chords(chords_); let mut stack = vec![chords[0].1]; for i in 1..N { let (A1, B1) = chords[i]; while !stack.is_empty() { let B = stack[stack.len()-1]; if A1 < B { if B1 > B { return true } else { stack.push(B1); break } } else { stack.pop(); } } if stack.is_empty() { stack.push(B1) } } false } fn main() { let chords = read_input(); println!("{}", YesNo(F(chords))) }