結果

問題 No.2353 Guardian Dogs in Spring
ユーザー atcoder8atcoder8
提出日時 2023-06-16 23:20:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,251 bytes
コンパイル時間 1,022 ms
コンパイル使用メモリ 153,728 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 22:21:09
合計ジャッジ時間 7,209 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 7 ms
4,384 KB
testcase_06 AC 7 ms
4,380 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 8 ms
4,380 KB
testcase_09 AC 8 ms
4,384 KB
testcase_10 AC 8 ms
4,384 KB
testcase_11 AC 8 ms
4,380 KB
testcase_12 AC 8 ms
4,384 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 8 ms
4,384 KB
testcase_16 AC 8 ms
4,384 KB
testcase_17 AC 9 ms
4,380 KB
testcase_18 AC 9 ms
4,380 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 7 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 5 ms
4,384 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 5 ms
4,380 KB
testcase_32 AC 7 ms
4,380 KB
testcase_33 AC 8 ms
4,384 KB
testcase_34 AC 8 ms
4,384 KB
testcase_35 AC 8 ms
4,384 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 3 ms
4,384 KB
testcase_40 AC 5 ms
4,384 KB
testcase_41 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use amplitude::Amplitude;

fn main() {
    let n = {
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).unwrap();
        line.trim().parse::<usize>().unwrap()
    };
    let xy: Vec<_> = (0..n)
        .map(|_| {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                iter.next().unwrap().parse::<usize>().unwrap(),
                iter.next().unwrap().parse::<usize>().unwrap(),
            )
        })
        .collect();

    let mut amplitudes: Vec<_> = xy
        .iter()
        .enumerate()
        .map(|(i, &(x, y))| (Amplitude::new(x, y), i))
        .collect();
    amplitudes.sort_unstable_by_key(|x| x.0);

    let mut ans = vec![];
    for i in (0..(n - 1)).step_by(2) {
        ans.push((amplitudes[i].1, amplitudes[i + 1].1))
    }

    println!("{}", ans.len());
    for e in ans {
        println!("{} {}", e.0 + 1, e.1 + 1);
    }
}

pub mod amplitude {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct Amplitude {
        pub x: usize,
        pub y: usize,
    }

    impl PartialOrd for Amplitude {
        fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
            let sq_dist_1 = self.x.pow(2) + self.y.pow(2);
            let sq_dist_2 = other.x.pow(2) + other.y.pow(2);

            if self.x == 0 && other.x == 0 {
                sq_dist_1.partial_cmp(&sq_dist_2)
            } else if self.x == 0 {
                Some(std::cmp::Ordering::Greater)
            } else if other.x == 0 {
                Some(std::cmp::Ordering::Less)
            } else {
                let frac1 = other.x * self.y;
                let frac2 = self.x * other.y;

                if frac1 == frac2 {
                    sq_dist_1.partial_cmp(&sq_dist_2)
                } else {
                    frac1.partial_cmp(&frac2)
                }
            }
        }
    }

    impl Ord for Amplitude {
        fn cmp(&self, other: &Self) -> std::cmp::Ordering {
            self.partial_cmp(other).unwrap()
        }
    }

    impl Amplitude {
        pub fn new(x: usize, y: usize) -> Self {
            Self { x, y }
        }
    }
}
0