結果

問題 No.545 ママの大事な二人の子供
ユーザー hatoohatoo
提出日時 2017-10-12 22:29:00
言語 Rust
(1.77.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 3,751 bytes
コンパイル時間 13,120 ms
コンパイル使用メモリ 377,836 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 17:09:36
合計ジャッジ時間 14,640 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 0 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 0 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 10 ms
5,376 KB
testcase_25 AC 22 ms
5,376 KB
testcase_26 AC 20 ms
5,376 KB
testcase_27 AC 19 ms
5,376 KB
testcase_28 AC 22 ms
5,376 KB
testcase_29 AC 22 ms
5,376 KB
testcase_30 AC 23 ms
5,376 KB
testcase_31 AC 23 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn get<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }

    #[allow(dead_code)]
    pub fn get2<T: FromStr, U: FromStr>() -> (T, U)
    where
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }

    #[allow(dead_code)]
    pub fn get3<S: FromStr, T: FromStr, U: FromStr>() -> (S, T, U)
    where
        <S as FromStr>::Err: Debug,
        <T as FromStr>::Err: Debug,
        <U as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        let mut iter = line.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    }
}


#[allow(unused_macros)]
macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn main() {
    let n: usize = util::get();
    let ab: Vec<(i64, i64)> = (0..n).map(|_| util::get2()).collect();

    let b_sum = ab.iter().map(|t| t.1).sum::<i64>();
    let c: Vec<_> = ab.iter().map(|t| t.0 + t.1).collect();

    let l = &c[..n / 2];
    let r = &c[n / 2..];
    let mut l_comb = Vec::new();
    let mut r_comb = Vec::new();

    for i in 0..1 << l.len() {
        let mut sum = 0;
        for j in 0..l.len() {
            if i >> j & 1 == 1 {
                sum += l[j];
            }
        }
        l_comb.push(sum);
    }

    for i in 0..1 << r.len() {
        let mut sum = 0;
        for j in 0..r.len() {
            if i >> j & 1 == 1 {
                sum += r[j];
            }
        }
        r_comb.push(sum);
    }

    l_comb.sort();
    l_comb.dedup();
    r_comb.sort();
    r_comb.dedup();
    let mut ans = i64::max_value();

    for &x in &l_comb {
        if x > b_sum {
            ans = min(ans, x - b_sum);
        } else {
            let rest = b_sum - x;
            match r_comb.binary_search(&rest) {
                Ok(_) => {
                    ans = 0;
                    break;
                }
                Err(i) => {
                    if i >= r_comb.len() {
                        if r_comb.len() > 0 {
                            ans = min(ans, b_sum - x - r_comb[r_comb.len() - 1])
                        }
                    } else {
                        ans = min(ans, r_comb[i] + x - b_sum);
                        if i > 0 {
                            ans = min(ans, b_sum - r_comb[i - 1] - x);
                        }
                    }
                }
            }
        }
    }

    println!("{}", ans);
}
0