結果

問題 No.914 Omiyage
ユーザー fukafukatani
提出日時 2019-10-25 21:33:13
言語 Rust
(1.37.0)
結果
AC  
実行時間 37 ms
コード長 2,844 Byte
コンパイル時間 1,446 ms
使用メモリ 2,724 KB
最終ジャッジ日時 2019-10-26 23:55:47

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
99_system_test1.txt AC 30 ms
2,720 KB
99_system_test2.txt AC 37 ms
2,720 KB
99_system_test3.txt AC 32 ms
2,724 KB
corner1_1.txt AC 2 ms
956 KB
corner1_2.txt AC 2 ms
960 KB
corner1_3.txt AC 2 ms
956 KB
corner2_1.txt AC 1 ms
956 KB
corner2_2.txt AC 1 ms
960 KB
corner3_1.txt AC 2 ms
956 KB
corner3_2.txt AC 2 ms
956 KB
corner4_1.txt AC 5 ms
1,024 KB
large1_1.txt AC 32 ms
2,720 KB
large1_2.txt AC 32 ms
2,724 KB
rand1_1.txt AC 1 ms
956 KB
rand1_2.txt AC 2 ms
956 KB
rand1_3.txt AC 4 ms
964 KB
sample1_1.txt AC 2 ms
956 KB
sample1_2.txt AC 2 ms
960 KB
sample1_3.txt AC 2 ms
960 KB
テストケース一括ダウンロード
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:23:9
   |
23 |     for i in 0..n {
   |         ^ help: consider prefixing with an underscore: `_i`
   |
   = note: #[warn(unused_variables)] on by default

ソースコード

diff #
#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, m, k) = (v[0], v[1], v[2] as i64);

    let mut a = vec![];
    for i in 0..n {
        let v = read_vec::<i64>();
        a.push(v);
    }

    let mut cand1 = vec![];
    for i in 0..(m as i64).pow(n as u32 / 2) {
        let mut cand = 0;
        let mut i = i;
        for j in 0..(n / 2) {
            cand += a[j][i as usize % m];
            i /= m as i64;
        }
        cand1.push(cand);
    }
    let mut cand2 = vec![];
    for i in 0..(m as i64).pow(n as u32 - n as u32 / 2) {
        let mut cand = 0;
        let mut i = i;
        for j in n / 2..n {
            cand += a[j][i as usize % m];
            i /= m as i64;
        }
        cand2.push(cand);
    }
    cand2.sort();
    let mut ans = std::i64::MAX;
    for &c in cand1.iter() {
        let idx = cand2.upper_bound(&(k - c));
        if idx > 0 {
            ans = min(ans, k - c - cand2[idx - 1]);
        }
    }
    // debug!(cand1);
    // debug!(cand2);
    if ans == std::i64::MAX {
        println!("-1");
        return;
    }
    println!("{}", ans);
}

pub trait BinarySearch<T> {
    fn lower_bound(&self, x: &T) -> usize;
    fn upper_bound(&self, x: &T) -> usize;
}

impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }

    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();

        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0