結果

問題 No.545 ママの大事な二人の子供
ユーザー fukafukatanifukafukatani
提出日時 2018-11-21 23:23:08
言語 Rust
(1.77.0 + proconio)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,881 bytes
コンパイル時間 12,661 ms
コンパイル使用メモリ 387,484 KB
最終ジャッジ日時 2024-11-14 20:41:12
合計ジャッジ時間 13,426 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error: expected one of `:`, `@`, or `|`, found `)`
  --> src/main.rs:16:29
   |
16 |     fn lower_bound(&self, &T) -> usize;
   |                             ^ expected one of `:`, `@`, or `|`
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
   |
16 |     fn lower_bound(&self, T: &TypeName) -> usize;
   |                           ~~~~~~~~~~~~
help: if this is a type, explicitly ignore the parameter name
   |
16 |     fn lower_bound(&self, _: &T) -> usize;
   |                           ++

error: expected one of `:`, `@`, or `|`, found `)`
  --> src/main.rs:17:29
   |
17 |     fn upper_bound(&self, &T) -> usize;
   |                             ^ expected one of `:`, `@`, or `|`
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this is a parameter name, give it a type
   |
17 |     fn upper_bound(&self, T: &TypeName) -> usize;
   |                           ~~~~~~~~~~~~
help: if this is a type, explicitly ignore the parameter name
   |
17 |     fn upper_bound(&self, _: &T) -> usize;
   |                           ++

warning: unused import: `std::collections::*`
 --> src/main.rs:1:5
  |
1 | use std::collections::*;
  |     ^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error: could not compile `main` (bin "main") due to 2 previous errors; 1 warning emitted

ソースコード

diff #

use std::collections::*;
use std::cmp::Ordering;

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()
}

trait BinarySearch<T> {
    fn lower_bound(&self, &T) -> usize;
    fn upper_bound(&self, &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 main() {
    let n = read::<usize>();
    let mut a = vec![0; n];
    let mut b = vec![0; n];
    for i in 0..n {
        let v = read_vec::<i64>();
        a[i] = v[0];
        b[i] = v[1];
    }

    if n == 1 {
        println!("{:?}", std::cmp::min(a[0], b[0]));
        return;
    }

    let half = (n / 2) as u32;
    let mut half_cominations: Vec<i64> = Vec::new();

    for i in 0..2usize.pow(half) {
        let mut i = i;
        let mut num: i64 = 0;
        for j in 0..half {
            if i % 2 == 0 {
                num += a[j as usize];
            } else {
                num -= b[j as usize];
            }
            i /= 2;
        }
        half_cominations.push(num);
    }

    half_cominations.sort();

    let mut ans = std::i64::MAX;

    let latter_half = n as u32 - half;
    for i in 0..2usize.pow(latter_half) {
        let mut i = i;
        let mut num: i64 = 0;
        for j in half..n as u32 {
            if i % 2 == 0 {
                num += a[j as usize];
            } else {
                num -= b[j as usize];
            }
            i /= 2;
        }

        let lb = half_cominations.lower_bound(&-num);

        if lb > 0 {
            let upper = half_cominations[lb - 1] + num;
            ans = std::cmp::min(upper.abs(), ans);
        }
        if lb < half_cominations.len() {
            let lower = half_cominations[lb] + num;
            ans = std::cmp::min(lower.abs(), ans);
        }
    }

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