結果

問題 No.545 ママの大事な二人の子供
ユーザー snteasntea
提出日時 2017-09-11 21:48:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 3,275 bytes
コンパイル時間 2,127 ms
コンパイル使用メモリ 166,392 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 06:55:11
合計ジャッジ時間 3,653 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 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 1 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 10 ms
5,376 KB
testcase_23 AC 19 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 16 ms
5,376 KB
testcase_26 AC 19 ms
5,376 KB
testcase_27 AC 13 ms
5,376 KB
testcase_28 AC 20 ms
5,376 KB
testcase_29 AC 15 ms
5,376 KB
testcase_30 AC 14 ms
5,376 KB
testcase_31 AC 15 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused imports: `Ordering`, `max`
 --> main.rs:2:16
  |
2 | use std::cmp::{Ordering, min, max};
  |                ^^^^^^^^       ^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused macro definition: `parse`
  --> main.rs:19:14
   |
19 | macro_rules! parse {
   |              ^^^^^
   |
   = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `rvec`
  --> main.rs:44:14
   |
44 | macro_rules! rvec {
   |              ^^^^

warning: unused macro definition: `readlvec`
  --> main.rs:50:14
   |
50 | macro_rules! readlvec {
   |              ^^^^^^^^

warning: unused macro definition: `debug`
  --> main.rs:60:14
   |
60 | macro_rules! debug {
   |              ^^^^^

warning: function `printvec` is never used
  --> main.rs:66:4
   |
66 | fn printvec<T>(v: &Vec<T>) where T: std::fmt::Display {
   |    ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 6 warnings emitted

ソースコード

diff #

// use std::ops::{Index, IndexMut};
use std::cmp::{Ordering, min, max};
// use std::collections::{BinaryHeap, BTreeMap};
// use std::collections::btree_map::Entry::{Occupied, Vacant};
// use std::clone::Clone;

fn getline() -> String{
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

macro_rules! split {
    ($x: expr) => {
        ($x).trim().split(' ').collect()
    }
}

macro_rules! parse {
    ($x: expr) => {
        ($x).parse().unwrap()
    }
}

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            let iv: Vec<_> = split!(s);
            let mut iter = iv.into_iter();
            iter.next().unwrap().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let iv: Vec<_> = split!(s);
            let mut iter = iv.into_iter();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! rvec {
    ($x: expr) => {
        ($x).into_iter().map(|x| parse!(x)).collect()
    }
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iv: Vec<_> = split!(s);
            iv.into_iter().map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {}", stringify!($x), $x)
    }
}

fn printvec<T>(v: &Vec<T>) where T: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

fn abs(x: i64) -> i64 {
    if x < 0 {
        -x
    } else {
        x
    }
}

const INF: i64 = 1e18 as i64;

fn main() {
    let n = readl!(usize);
    let mut iv = vec![(0,0); n];
    for i in 0..n {
        iv[i] = readl!(i64, i64);
    }
    let (a, b): (Vec<_>, Vec<_>) = iv.into_iter().unzip();
    
    if n == 1 {
        println!("{}", min(a[0], b[0]));
        return;
    }

    let mut re = Vec::<i64>::new();
    re.reserve(1<<(n-n/2));
    for mask in 0..(1<<(n-n/2)) {
        let mut sum = 0;
        for i in 0..n-n/2 {
            if (mask >> i) & 1 == 1 {
                sum += a[n/2+i];
            } else {
                sum -= b[n/2+i];
            }
        }
        re.push(sum);
    }
    re.sort();
    // printvec(&re);

    let mut ans = INF;
    for mask in 0..1<<n/2 {
        let mut sum = 0;
        for i in 0..n/2 {
            if (mask>>i)&1 == 1 {
                sum += a[i];
            } else {
                sum -= b[i];
            }
        }
        // debug!(sum);
        if re[0] >= -sum {
            ans = min(ans, abs(re[0] + sum));
        } else if re[re.len()-1] < -sum {
            ans = min(ans, abs(re[re.len()-1] + sum));
        } else {
            let mut l = 0;
            let mut r = re.len();
            while l+1 < r {
                let m = (l+r)/2;
                if -sum <= re[m] {
                    r = m;
                } else {
                    l = m;
                }
            }
            // debug!(r);
            ans = min(ans, abs(re[r] + sum));
            if r > 0 {
                ans = min(ans, abs(re[r-1] + sum));
            }
        }
    }
    
    println!("{}", ans);
}

0