QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#378301#8572. Passing Gameucup-team635#WA 358ms34652kbRust5.2kb2024-04-06 11:00:152024-04-06 11:00:17

Judging History

你现在查看的是最新测评结果

  • [2024-04-06 11:00:17]
  • 评测
  • 测评结果:WA
  • 用时:358ms
  • 内存:34652kb
  • [2024-04-06 11:00:15]
  • 提交

answer

// ---------- 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 ----------
// ---------- begin Li Chao Tree ----------
// verify: https://old.yosupo.jp/submission/66738
#[derive(Clone)]
pub struct Line {
    a: i64,
    b: i64,
}

impl Line {
    pub fn new(a: i64, b: i64) -> Self {
        Line { a, b }
    }
    pub fn eval(&self, x: i64) -> i64 {
        self.a * x + self.b
    }
}

pub struct LiChaoTree(Vec<(i64, Line)>);

impl LiChaoTree {
    fn inorder(&mut self, v: usize, point: &[i64], x: &mut usize) {
        if v >= self.0.len() {
            return;
        }
        self.inorder(2 * v + 1, point, x);
        self.0[v].0 = point[*x];
        *x += 1;
        self.inorder(2 * v + 2, point, x);
    }
    pub fn new(mut point: Vec<i64>) -> Self {
        assert!(point.len() > 0);
        point.sort();
        point.dedup();
        let mut cht = Self(vec![(0, Line::new(0, std::i64::MAX)); point.len()]);
        cht.inorder(0, &point, &mut 0);
        cht
    }
    pub fn insert(&mut self, mut line: Line) {
        let mut v = 0;
        while let Some(p) = self.0.get_mut(v) {
            if p.1.eval(p.0) > line.eval(p.0) {
                std::mem::swap(&mut p.1, &mut line);
            }
            v = 2 * v + if line.a > p.1.a { 1 } else { 2 };
        }
    }
    pub fn find(&self, x: i64) -> i64 {
        let mut res = std::i64::MAX;
        let mut v = 0;
        while let Some(p) = self.0.get(v) {
            res = res.min(p.1.eval(x));
            v = 2 * v + if x < p.0 { 1 } else { 2 }
        }
        res
    }
}
// ---------- end Li Chao Tree ----------
// ---------- 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);
}

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 x: Vec<i64> = sc.next_vec::<i64>(n);
        let s: Vec<i64> = sc.next_vec::<i64>(n);
        let ans = solve(k, x, s);
        writeln!(out, "{}", ans).ok();
    }
}

fn solve(k: usize, x: Vec<i64>, s: Vec<i64>) -> i64 {
    let n = x.len();
    let mut ord = (0..n).collect::<Vec<_>>();
    ord.sort_by_key(|p| x[*p]);
    let e = ord.iter().map(|e| (x[*e], s[*e])).collect::<Vec<_>>();
    let src = ord.iter().position(|x| *x == 0).unwrap();
    let dst = ord.iter().position(|x| *x == n - 1).unwrap();
    let mut ans = std::i64::MAX;
    for i in 0..2 {
        let inf = std::i64::MAX / 2;
        let mut dp = vec![inf; n];
        dp[src] = 0;
        let point = e.iter().map(|e| e.0).collect::<Vec<_>>();
        for j in 0..=k {
            let mut cht = LiChaoTree::new(point.clone());
            let mut update = false;
            if (i + j) % 2 == 0 {
                for (i, &(x, s)) in e.iter().enumerate() {
                    let val = cht.find(x);
                    update |= dp[i].chmin(val);
                    if dp[i] != inf {
                        cht.insert(Line::new(s, dp[i] - s * x));
                    }
                }
            } else {
                for (i, &(x, s)) in e.iter().enumerate().rev() {
                    let val = cht.find(-x);
                    update |= dp[i].chmin(val);
                    if dp[i] != inf {
                        cht.insert(Line::new(s, dp[i] + s * x));
                    }
                }
            }
            if !update {
                break;
            }
        }
        ans = ans.min(dp[dst]);
    }
    ans
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 2084kb

input:

2
4 2
3 2 1 6
3 1 1 3
2 0
1 2
1 2

output:

7
1

result:

ok 2 number(s): "7 1"

Test #2:

score: 0
Accepted
time: 312ms
memory: 33544kb

input:

1
300000 204334
809492393 304618667 173130445 377106790 364888630 949045125 622060683 557772818 216607577 848817467 862855568 507840723 120816645 639713488 741781998 682531787 685261161 601686403 355792373 162819930 710057718 234560726 998604853 678957602 485413982 855985802 109303681 979706626 4822...

output:

31313390701066

result:

ok 1 number(s): "31313390701066"

Test #3:

score: 0
Accepted
time: 302ms
memory: 16560kb

input:

3
100000 65460
217141764 710454586 789075415 24849107 685675008 839804815 638763480 327755609 43827967 390187172 301370841 622696676 598237196 232099091 211987715 416876077 572665966 73382836 520033984 808399404 752832432 341795744 434460344 535426588 136624537 997406768 297342165 558882675 26863877...

output:

70635841128944
47230361360721
59110547802683

result:

ok 3 number(s): "70635841128944 47230361360721 59110547802683"

Test #4:

score: -100
Wrong Answer
time: 358ms
memory: 34652kb

input:

1
300000 101975
207258305 525434317 528778163 645316642 562113679 143398489 9114413 669854123 106324041 841914487 21419012 308025536 689200225 263298218 39377353 860366080 24610184 43404209 529054797 902238799 422737070 484129934 967667618 953541323 338625285 115085955 363490839 998893783 877857789 ...

output:

44464396812195

result:

wrong answer 1st numbers differ - expected: '40311829457542', found: '44464396812195'