結果

問題 No.914 Omiyage
ユーザー fukafukatanifukafukatani
提出日時 2019-10-25 21:33:13
言語 Rust
(1.44.1)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 2,844 Byte
コンパイル時間 3,593 ms
使用メモリ 8,940 KB
最終ジャッジ日時 2020-07-06 05:09:10

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 29 ms
8,940 KB
testcase_01 AC 37 ms
8,896 KB
testcase_02 AC 29 ms
6,892 KB
testcase_03 AC 29 ms
8,940 KB
testcase_04 AC 29 ms
6,900 KB
testcase_05 AC 1 ms
6,892 KB
testcase_06 AC 1 ms
8,900 KB
testcase_07 AC 1 ms
6,896 KB
testcase_08 AC 1 ms
6,896 KB
testcase_09 AC 1 ms
8,900 KB
testcase_10 AC 1 ms
6,896 KB
testcase_11 AC 2 ms
8,940 KB
testcase_12 AC 3 ms
8,936 KB
testcase_13 AC 30 ms
8,936 KB
testcase_14 AC 28 ms
6,896 KB
testcase_15 AC 2 ms
8,936 KB
testcase_16 AC 2 ms
6,900 KB
testcase_17 AC 2 ms
6,896 KB
testcase_18 AC 1 ms
8,940 KB
testcase_19 AC 1 ms
8,936 KB
testcase_20 AC 1 ms
6,896 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:23:9
   |
23 |     for i in 0..n {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

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