結果

問題 No.497 入れ子の箱
ユーザー cympfhcympfh
提出日時 2017-06-21 21:36:49
言語 Rust
(1.77.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 3,560 bytes
コンパイル時間 1,085 ms
コンパイル使用メモリ 170,216 KB
実行使用メモリ 13,824 KB
最終ジャッジ日時 2024-04-10 09:59:26
合計ジャッジ時間 2,774 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 21 ms
13,824 KB
testcase_04 AC 21 ms
13,696 KB
testcase_05 AC 20 ms
13,696 KB
testcase_06 AC 20 ms
13,696 KB
testcase_07 AC 26 ms
13,824 KB
testcase_08 AC 25 ms
13,824 KB
testcase_09 AC 26 ms
13,824 KB
testcase_10 AC 27 ms
13,824 KB
testcase_11 AC 26 ms
13,696 KB
testcase_12 AC 26 ms
8,832 KB
testcase_13 AC 26 ms
8,576 KB
testcase_14 AC 26 ms
8,576 KB
testcase_15 AC 26 ms
8,704 KB
testcase_16 AC 27 ms
8,832 KB
testcase_17 AC 25 ms
8,576 KB
testcase_18 AC 18 ms
13,568 KB
testcase_19 AC 17 ms
13,568 KB
testcase_20 AC 17 ms
13,568 KB
testcase_21 AC 17 ms
13,568 KB
testcase_22 AC 17 ms
13,568 KB
testcase_23 AC 4 ms
6,944 KB
testcase_24 AC 5 ms
6,940 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 24 ms
10,752 KB
testcase_28 AC 24 ms
10,752 KB
testcase_29 AC 24 ms
10,624 KB
testcase_30 AC 9 ms
6,940 KB
testcase_31 AC 9 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
use std::io::{ self, Write };
use std::str::FromStr;
use std::cmp::{ min, max };
use std::collections::{ BinaryHeap, VecDeque };

#[allow(unused_macros)]
macro_rules! trace {
    ($var:expr) => ({
        let _ = writeln!(&mut std::io::stderr(), ">>> {} = {:?}", stringify!($var), $var);
    })
}
#[allow(unused_macros)]
macro_rules! swap { ($a:expr, $b:expr) => ({ let t = $b; $b = $a; $a = t; }) }

#[derive(Debug)]
struct Topological(Vec<usize>);

impl Topological {

    fn visit(u: usize, mut used: &mut Vec<bool>, rd: &Vec<Vec<usize>>, mut ord: &mut Vec<usize>) {
        if used[u] { return }
        used[u] = true;
        for &v in rd[u].iter() {
            Self::visit(v, &mut used, &rd, &mut ord);
        }
        ord.push(u);
    }

    fn new(neigh: &Vec<Vec<usize>>) -> Self {
        let n = neigh.len();
        let mut rd = vec![vec![]; n];
        for u in 0..n {
            for &v in neigh[u].iter() {
                rd[v].push(u);
            }
        }
        // sort
        let mut used = vec![false; n];
        let mut ord = vec![];
        for u in 0..n { Self::visit(u, &mut used, &rd, &mut ord); }
        Topological(ord)
    }
}

struct Hako(i32, i32, i32);

fn can_include(a: &Hako, b: &Hako) -> bool {
    let Hako(x1, y1, z1) = *a;
    let Hako(x2, y2, z2) = *b;
    if x1 > x2 && y1 > y2 && z1 > z2 { return true }
    if x1 > x2 && y1 > z2 && z1 > y2 { return true }
    if x1 > y2 && y1 > x2 && z1 > z2 { return true }
    if x1 > y2 && y1 > z2 && z1 > x2 { return true }
    if x1 > z2 && y1 > x2 && z1 > y2 { return true }
    if x1 > z2 && y1 > y2 && z1 > x2 { return true }
    false
}

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let mut hakos = vec![];
    for _ in 0..n {
        let x: i32 = sc.cin();
        let y: i32 = sc.cin();
        let z: i32 = sc.cin();
        hakos.push(Hako(x, y, z));
    }

    let mut neigh = vec![vec![]; n];
    let mut dmat = vec![vec![false; n]; n];

    for i in 0..n {
        for j in 0..n {
            if can_include(&hakos[i], &hakos[j]) {
                neigh[i].push(j);
                dmat[i][j] = true;
            }
        }
    }

    let Topological(ord) = Topological::new(&neigh);
    let mut memo = vec![1; n];

    for i in 0..n {
        for j in (i + 1)..n {
            if dmat[ord[i]][ord[j]] {
                memo[j] = max(memo[j], memo[i] + 1);
            }
        }
    }

    let mut maxlen = 0;
    for i in 0..n {
        maxlen = max(maxlen, memo[i]);
    }

    println!("{}", maxlen);

}

#[allow(dead_code)]
struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn reserve(&mut self) {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        self.reserve();
        match self.buffer.pop_front().unwrap().parse::<T>() {
            Ok(a) => a,
            Err(_) => panic!("parse err")
        }
    }
    fn get_char(&mut self) -> char {
        self.reserve();
        let head = self.buffer[0].chars().nth(0).unwrap();
        let tail = String::from( &self.buffer[0][1..] );
        if tail.len()>0 { self.buffer[0]=tail } else { self.buffer.pop_front(); }
        head
    }
}
0