結果

問題 No.506 限られたジャパリまん
ユーザー cympfhcympfh
提出日時 2017-05-21 15:26:52
言語 Rust
(1.77.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 3,899 bytes
コンパイル時間 4,753 ms
コンパイル使用メモリ 155,456 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-10 12:54:22
合計ジャッジ時間 2,639 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 9 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 8 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused macro definition: `trace`
 --> Main.rs:7:14
  |
7 | macro_rules! trace {
  |              ^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `swap`
  --> Main.rs:12:14
   |
12 | macro_rules! swap { ($a:expr, $b:expr) => ({ let t = $b; $b = $a; $a = t; }) }
   |              ^^^^

warning: 2 warnings emitted

ソースコード

diff #

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

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

const M: i64 = 1000000007;

struct Combination { n: usize, m: usize, ar: Vec<usize> }
impl Combination {
    fn new(n: usize, m: usize) -> Combination {
        let mut ar = vec![0; m];
        for i in 0..m { ar[i] = m - i - 1 }
        Combination { n: n, m: m, ar: ar }
    }
}
impl Iterator for Combination {
    type Item = Vec<usize>;
    fn next(&mut self) -> Option<Vec<usize>> {
        if self.m == 0 {
            if self.n == 0 {
                return None
            } else {
                self.n = 0;
                return Some(vec![]);
            }
        }
        if self.ar[self.m-1] > self.n - self.m { return None }
        let r = self.ar.clone();
        self.ar[0] += 1;
        let mut c = 0;
        for i in 0..self.m-1 {
            if self.ar[i] >= self.n - i {
                self.ar[i + 1] += 1;
                c = i + 1;
            } else {
                break;
            }
        }
        for i in (0..c).rev() {
            self.ar[i] = self.ar[i+1] + 1;
        }
        return Some(r);
    }
}

fn main() {
    let mut sc = Scanner::new();
    let h: usize = sc.cin();
    let w: usize = sc.cin();
    let k: usize = sc.cin();
    let p: usize = sc.cin();

    let mut f = vec![vec![false; w + 1]; h + 1];
    let mut blocks = vec![];
    for _ in 0..k {
        let x: usize = sc.cin();
        let y: usize = sc.cin();
        let name: String = sc.cin();
        blocks.push((x, y, name));
        f[x][y] = true;
    }

    let mut ans: i64 = 0;
    let mut opt_indices = vec![];
    let mut memo = vec![vec![0; w + 1]; h + 1];
    for ar in Combination::new(k, p) {
        for &i in ar.iter() { let (x, y, _) = blocks[i]; f[x][y] = false; }
        for i in 0..(h + 1) {
            for j in 0..(w + 1) {
                if f[i][j] {
                    memo[i][j] = 0;
                } else if i == 0 && j == 0 {
                    memo[0][0] = 1;
                } else if i == 0 {
                    memo[0][j] = memo[0][j - 1];
                } else if j == 0 {
                    memo[i][0] = memo[i - 1][0];
                } else {
                    memo[i][j] = memo[i - 1][j] + memo[i][j - 1];
                }
            }
        }
        if ans < memo[h][w] {
            ans = memo[h][w];
            opt_indices = ar.clone();
        }
        for &i in ar.iter() { let (x, y, _) = blocks[i]; f[x][y] = true; }
    }

    println!("{}", ans % M);
    opt_indices.sort();
    for &i in opt_indices.iter() {
        println!("{}", blocks[i].2);
    }
}

#[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