結果

問題 No.2465 Dilated Water Simulation
ユーザー 👑 NachiaNachia
提出日時 2023-09-10 12:29:37
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 4,771 bytes
コンパイル時間 12,872 ms
コンパイル使用メモリ 378,632 KB
実行使用メモリ 15,464 KB
最終ジャッジ日時 2024-06-28 03:50:17
合計ジャッジ時間 16,273 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 52 ms
14,872 KB
testcase_06 AC 49 ms
14,792 KB
testcase_07 AC 53 ms
14,852 KB
testcase_08 AC 49 ms
14,956 KB
testcase_09 AC 56 ms
14,860 KB
testcase_10 AC 50 ms
14,812 KB
testcase_11 AC 51 ms
14,840 KB
testcase_12 AC 49 ms
14,932 KB
testcase_13 AC 51 ms
14,836 KB
testcase_14 AC 50 ms
14,808 KB
testcase_15 AC 49 ms
14,876 KB
testcase_16 AC 51 ms
14,844 KB
testcase_17 AC 48 ms
14,820 KB
testcase_18 AC 51 ms
14,852 KB
testcase_19 AC 49 ms
14,864 KB
testcase_20 AC 51 ms
14,984 KB
testcase_21 AC 51 ms
15,112 KB
testcase_22 AC 52 ms
15,464 KB
testcase_23 AC 53 ms
15,292 KB
testcase_24 AC 53 ms
15,120 KB
testcase_25 AC 52 ms
15,076 KB
testcase_26 AC 56 ms
15,420 KB
testcase_27 AC 56 ms
15,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn solve(v : Vec<i64>, n : i64) -> Vec<i64> {
	use std::cmp::min;

	let mut v = v.clone();

	let l = v.len();
    let ncycles = n / (l as i64);
    
    let mut gv : Vec<i64> = vec![v[0]; l];
    for i in 1..l { gv[i] = min(gv[i-1], v[i]); }
    let minv = gv[l-1];
    for i in 0..l { v[i] -= minv; }
    for i in 0..l { gv[i] -= minv; }

    let mut height_length = vec![(0i64, 0i64); 0];
    let mut imos1 = vec![0i64; l+1];
    let mut imos0 = vec![0i64; l];
    let mask = (1i64 << 30) - 1;

    let mut contribute = | pos : usize, t : i64, weight : i64 | {
        if t + (pos as i64) <= ncycles {
            imos0[pos] += weight * (ncycles - t);
            imos0[pos] &= mask;
            imos1[pos] -= weight;
            imos1[pos] &= mask;
        }
        else if t <= ncycles {
            let decrease_len = (ncycles - t) as usize;
            imos0[pos] += weight * (ncycles - t);
            imos0[pos] &= mask;
            imos1[pos] -= weight;
            imos1[pos] &= mask;
            imos1[pos - decrease_len] += weight;
            imos1[pos - decrease_len] &= mask;
        }
    };

    for i in (0..l).rev() {
        if gv[i] == 0 { continue; }

        let mut rem = v[i];
        let mut turn = 1i64;
        
        while !height_length.is_empty() && rem >= gv[i] {
            let (h, len) = height_length.pop().unwrap();
            contribute(i, turn, -h);
            contribute(i, turn + len, h);
            
            if len == 0 { continue; }
            if h == gv[i] {
                turn += len;
                continue;
            }

            let hturn = (rem - gv[i]) / (gv[i] - h) + 1;
            if hturn >= len {
                turn += len;
                rem -= len * (gv[i] - h);
            }
            else {
                rem -= hturn * (gv[i] - h);
                contribute(i, turn + hturn, h);
                contribute(i, turn + len, -h);
                turn += hturn;
                height_length.push((h, len - hturn));
                break;
            }
        }

        turn += rem / gv[i];
        rem %= gv[i];
        
        contribute(i, turn - 1, rem);
        contribute(i, turn, -rem);
        height_length.push((rem, 1));
        turn -= 1;
        
        contribute(i, 0, gv[i]);
        contribute(i, turn, -gv[i]);
        height_length.push((gv[i], turn));
    }

    for i in (0..l).rev() { imos1[i] = (imos1[i] + imos1[i+1]) & mask; }
    for i in 0..l { imos0[i] = (imos0[i] + imos1[i+1]) & mask; }
    imos0[0] = v[0];

    let mut ans = vec![0i64; l];
    let mut rem = v[0];
    for i in (0..l).rev() {
        let x = min(imos0[i], rem);
        rem -= x;
        ans[i] = x;
    }
    ans[0] += minv;

    for i in 0..((n % (l as i64)) as usize) {
        let f = min(ans[i], v[i+1] + minv - ans[i+1]);
        ans[i] -= f;
        ans[i+1] += f;
    }
	
	ans
}

fn main(){
    let mut cin = StdIStr::new();
    let n : usize = cin.get();
    let v : Vec<i64> = cin.vec(n);
    let m : i64 = cin.get();
    let w = solve(v, m);
	use std::io::Write;
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    writeln!(out, "{}", w.iter().map(std::string::ToString::to_string).collect::<Vec<_>>().join(" ")).unwrap();
}



pub trait IStr {
	fn sc(&mut self) -> u8;
	fn gc(&mut self) -> u8;
	fn end(&mut self) -> bool;
	fn skipwc(&mut self){ while self.sc() <= (' ' as u8) { self.gc(); } }
}

pub trait FromIStr<T> { fn rd<Str : IStr>(s : &mut Str) -> T; }

macro_rules! impl_uint_from_istr {
	($t:ty) => {
		impl FromIStr<$t> for $t {
            fn rd<Str : IStr>(s : &mut Str) -> $t {
                s.skipwc();
                let mut x = 0 as $t;
                while ('0' as u8) <= s.sc() { x = x*10 + (s.gc() - '0' as u8) as $t; }
                x
            }
        }
	};
}

impl_uint_from_istr!(i64);
impl_uint_from_istr!(usize);

struct StdIStr { buf : [u8; 65536], idx : usize, fil : usize, eof : bool }

impl IStr for StdIStr {
	fn sc(&mut self) -> u8 { if self.eof { 0u8 } else { self.buf[self.idx] } }
	fn gc(&mut self) -> u8 {
		let res = self.sc(); self.idx += 1;
		if self.idx == self.fil { self.proc(); }
		res
	}
	fn end(&mut self) -> bool { self.eof }
}

impl StdIStr {
	fn proc(&mut self) {
		use std::io::Read;
		self.fil = std::io::stdin().read(&mut self.buf).unwrap();
		self.eof = self.fil == 0;
		self.idx = 0;
		if self.eof { self.buf[0] = 0u8; }
	}
	pub fn new() -> StdIStr {
		let mut res = StdIStr{ buf : [0u8; 65536], idx : 0, fil : 0, eof : false };
		res.proc();
		res
	}
	pub fn get<T : FromIStr<T>>(&mut self) -> T { T::rd(self) }
	pub fn vec<T : FromIStr<T>>(&mut self, n : usize) -> Vec<T> {
		let mut r = Vec::<T>::new();
		r.reserve(n);
		for _ in 0..n { r.push(self.get()); }
		r
	}
}
0