結果

問題 No.3322 引っ張りだこ
コンテスト
ユーザー akakimidori
提出日時 2025-10-31 21:10:14
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 2,985 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 12,487 ms
コンパイル使用メモリ 399,376 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2025-10-31 21:10:34
合計ジャッジ時間 18,742 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 WA * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: type alias `Map` is never used
  --> src/main.rs:79:6
   |
79 | type Map<K, V> = BTreeMap<K, V>;
   |      ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: type alias `Set` is never used
  --> src/main.rs:80:6
   |
80 | type Set<T> = BTreeSet<T>;
   |      ^^^

warning: type alias `Deque` is never used
  --> src/main.rs:81:6
   |
81 | type Deque<T> = VecDeque<T>;
   |      ^^^^^

ソースコード

diff #
raw source code

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let t: u32 = sc.next();
    for _ in 0..t {
        let n: usize = sc.next();
        let k: usize = sc.next();
        let a: Vec<i64> = sc.next_vec(n);
        let b: Vec<i64> = sc.next_vec(n);
        let ans = solve(a, b, k);
        writeln!(out, "{}", ans).ok();
    }
}

fn solve(a: Vec<i64>, b: Vec<i64>, k: usize) -> i64 {
    let inf = std::i64::MAX / 2;
    let eval = |m: i64| -> (i64, usize) {
        let mut dp = [(-inf, 0); 2];
        dp[0] = (0, 0);
        for (&a, &b) in a.iter().zip(b.iter()) {
            let mut next = [(-inf, 0); 2];
            for (i, dp) in dp.iter().enumerate() {
                next[i].chmax(*dp);
                next[i ^ 1].chmax((dp.0 - m, dp.1 + 1));
            }
            next[0].0 += a;
            next[1].0 += b;
            dp = next;
        }
        dp[0].max(dp[1])
    };
    if eval(0).1 <= k {
        return eval(0).0;
    }
    let mut l = 0;
    let mut r = inf - 1;
    while r - l > 1 {
        let m = (l + r) / 2;
        if eval(m).1 > k {
            l = m;
        } else {
            r = m;
        }
    }
    eval(r).0 + k as i64 * r
}

// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::io::Write;
use std::collections::*;

type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}
// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
0