Submission #1690118


Source Code Expand

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B, C, D);

// ===

trait Monoid {
    fn mul(&self, Self) -> Self;

    fn one() -> Self;
}

struct SegmentTree<T> {
    n: usize,
    data: Vec<T>
}

impl<T: Monoid + Clone> SegmentTree<T> {
    fn new(n: usize, initial: T) -> Self {
        let vs = (n-1).next_power_of_two() << 1;
        SegmentTree { n: n, data: vec![initial; vs] }
    }

    fn new_with(v: &Vec<T>) -> Self {
        let vs = max(4, (v.len()-1).next_power_of_two() << 1);
        let n = v.len();
        let mut data: Vec<T> = vec![T::one(); vs];

        let bottom = vs/2-1;
        for i in 0..n {
            data[bottom+i] = v[i].clone();
        }
        for i in (0..bottom).rev() {
            data[i] = data[i*2+1].mul(data[i*2+2].clone());
        }
        SegmentTree { n: v.len(), data: data }
    }

    fn change(&mut self, idx: usize, new_value: T) {
        let mut pos = self.data.len() / 2 - 1 + idx;
        self.data[pos] = new_value;
        while pos >= 1 {
            let to = (pos - 1) / 2;
            self.data[to] = self.data[to*2+1].mul(self.data[to*2+2].clone());
            pos = to;
        }
    }

    fn range(&self, l: usize, r: usize) -> T {
        self.range2(l, r, 0, 0, self.data.len() / 2)
    }

    fn range2(&self, l: usize, r: usize, idx: usize, segl: usize, segr: usize) -> T {
        if r <= segl || segr <= l {
            return T::one()
        }
        if l <= segl && segr <= r {
            return self.data[idx].clone()
        }
        let med = (segl + segr) / 2;
        self.range2(l, r, idx*2+1, segl, med).mul(self.range2(l, r, idx*2+2, med, segr))
    }
}


// ===

#[derive(Clone, Debug, Copy)]
struct I64(i64);

impl Monoid for I64 {
    fn mul(&self, other: Self) -> Self {
        if self.0 < other.0 { I64(self.0) } else { I64(other.0) }
    }

    fn one() -> Self { I64(1e15 as i64) }
}

// ===

fn abs(a: usize, b: usize) -> i64 {
    (if a < b {
        b - a
    } else {
        a - b
    }) as i64
}

fn main() {
    let (n, q, mut a, mut b): (usize, usize, usize, usize) = read();
    a -= 1;
    b -= 1;
    let mut x: Vec<usize> = readnc();
    for i in 0..q {
        x[i] -= 1;
    }
    let mut last = a;

    let mut left = SegmentTree::new(n+10, I64::one());
    let mut right = SegmentTree::new(n+10, I64::one());
    for i in 0..n {
        let lcost = i as i64;
        let rcost = (n - 1 - i) as i64;
        left.change(i, I64(abs(b, i) + lcost));
        right.change(i, I64(abs(b, i) + rcost));
    }

    let mut add_all = 0;
    let mut dp: Vec<i64> = vec![0; n];
    for i in 0..n {
        dp[i] = abs(b, i);
    }

    let mut add_all = 0;
    for i in 0..q {
        let add = abs(last, x[i]);
        let tlast = dp[last] + add_all + add;
        let mut tt = I64::one().0;

        let lcost = x[i] as i64;
        let rcost = (n - 1 - x[i]) as i64;
        tt = min(tt, left.range(x[i], n).0 - lcost + add_all);
        tt = min(tt, right.range(0, x[i]+1).0 - rcost + add_all);
        add_all += add;
        dp[last] = min(tt, tlast) - add_all;


        let lcost = last as i64;
        let rcost = (n - 1 - last) as i64;
        left.change(last, I64(dp[last] + lcost));
        right.change(last, I64(dp[last] + rcost));

        // println!("{:?} {}", dp, add_all);

        last = x[i];
    }

    let mut best = I64::one().0;
    for i in 0..n {
        best = min(best, dp[i] + add_all);
    }
    println!("{}", best);
}

Submission Info

Submission Time
Task F - Many Moves
User hamadu
Language Rust (1.15.1)
Score 900
Code Size 4808 Byte
Status AC
Exec Time 186 ms
Memory 17392 KB

Compile Error

warning: variable does not need to be mutable, #[warn(unused_mut)] on by default
   --> ./Main.rs:162:9
    |
162 |     let mut add_all = 0;
    |         ^^^^^^^^^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 4
AC × 34
Set Name Test Cases
Sample example0, example1, example2, example3
All example0, example1, example2, example3, flower0, flower1, flower2, flower3, flower4, flower5, flower6, flower7, flower8, flower9, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, smallrand0, smallrand1, smallrand2, smallrand3, smallrand4, smallrand5, smallrand6, smallrand7, smallrand8, smallrand9
Case Name Status Exec Time Memory
example0 AC 2 ms 4352 KB
example1 AC 2 ms 4352 KB
example2 AC 2 ms 4352 KB
example3 AC 2 ms 4352 KB
flower0 AC 163 ms 17392 KB
flower1 AC 164 ms 17392 KB
flower2 AC 163 ms 17392 KB
flower3 AC 163 ms 17392 KB
flower4 AC 162 ms 17392 KB
flower5 AC 162 ms 17392 KB
flower6 AC 164 ms 17392 KB
flower7 AC 163 ms 17392 KB
flower8 AC 164 ms 17392 KB
flower9 AC 164 ms 17392 KB
maxrand0 AC 186 ms 17136 KB
maxrand1 AC 167 ms 17136 KB
maxrand2 AC 170 ms 17136 KB
maxrand3 AC 169 ms 17136 KB
maxrand4 AC 176 ms 17136 KB
maxrand5 AC 170 ms 17136 KB
maxrand6 AC 170 ms 17136 KB
maxrand7 AC 168 ms 17136 KB
maxrand8 AC 171 ms 17136 KB
maxrand9 AC 167 ms 17136 KB
smallrand0 AC 2 ms 4352 KB
smallrand1 AC 2 ms 4352 KB
smallrand2 AC 2 ms 4352 KB
smallrand3 AC 2 ms 4352 KB
smallrand4 AC 2 ms 4352 KB
smallrand5 AC 2 ms 4352 KB
smallrand6 AC 2 ms 4352 KB
smallrand7 AC 2 ms 4352 KB
smallrand8 AC 2 ms 4352 KB
smallrand9 AC 2 ms 4352 KB