結果

問題 No.545 ママの大事な二人の子供
ユーザー phsplsphspls
提出日時 2022-11-27 18:58:52
言語 Rust
(1.77.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,519 bytes
コンパイル時間 1,421 ms
コンパイル使用メモリ 170,352 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 01:36:13
合計ジャッジ時間 2,896 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

const INF: isize = 1isize << 60;

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut a = Vec::with_capacity(n);
    let mut b = Vec::with_capacity(n);
    for _ in 0..n {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<isize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        a.push(temp[0]);
        b.push(temp[1]);
    }

    let mut lefts = vec![];
    let mut rights = vec![];
    for pat in 0..(1<<(n/2)) {
        let val = (0..n/2).map(|i| if ((pat >> i) & 1) == 1 { a[i] } else { -b[i] }).sum::<isize>();
        lefts.push(val);
    }
    for pat in 0..(1 << (n-n/2)) {
        let val = (0..(n-n/2)).map(|i| if ((pat >> i) & 1) == 1 { a[i+n/2] } else { -b[i+n/2] }).sum::<isize>();
        rights.push(val);
    }
    lefts.sort();
    rights.sort();
    let mut result = INF;
    for &v in lefts.iter() {
        let target = -v;
        let mut lower = 0usize;
        let mut upper = rights.len()-1;
        while upper > lower {
            let middle = (upper + lower) / 2;
            if rights[middle] >= target {
                upper = middle;
            } else {
                lower = middle+1;
            }
        }
        result = result.min((v + rights[upper]).abs());
        if upper > 0 {
            result = result.min((v + rights[upper-1]).abs());
        }
    }
    println!("{}", result);
}
0