From 9606f587dcf0b329041a1b4c12f5be55eea9858b Mon Sep 17 00:00:00 2001 From: yenru0 Date: Mon, 24 Nov 2025 15:52:30 +0900 Subject: [PATCH] complete 30885.rs --- storage/zeta/rs/completed/30885.rs | 121 +++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 storage/zeta/rs/completed/30885.rs diff --git a/storage/zeta/rs/completed/30885.rs b/storage/zeta/rs/completed/30885.rs new file mode 100644 index 0000000..1a8e117 --- /dev/null +++ b/storage/zeta/rs/completed/30885.rs @@ -0,0 +1,121 @@ +use std::io::stdin; +use std::ptr; + +struct Cell { + value: u64, + index: usize, + left: *mut Cell, + right: *mut Cell, +} + +impl Cell { + fn new(value: u64, index: usize) -> *mut Cell { + Box::into_raw(Box::new(Cell { + value, + index, + left: ptr::null_mut(), + right: ptr::null_mut(), + })) + } +} + +fn last_man_standing(mut head: *mut Cell, mut tail: *mut Cell) -> *mut Cell { + let mut cnt = unsafe { (*tail).index + 1 }; + let mut curr = head; + + while cnt > 1 { + curr = head; + + while true { + unsafe { + let left = (*curr).left; + let right = (*curr).right; + let v = (*curr).value; + + if !left.is_null() && (*left).value <= v { + // eat left + (*curr).value += (*left).value; + // remove left + let left_left = (*left).left; + if left == head { + head = curr; + } + + (*curr).left = left_left; + if !left_left.is_null() { + (*left_left).right = curr; + } + cnt -= 1; + } + if !right.is_null() && (*right).value <= v { + // eat right + (*curr).value += (*right).value; + // remove right + let right_right = (*right).right; + if ptr::eq(right, tail) { + tail = curr; + } + (*curr).right = right_right; + if !right_right.is_null() { + (*right_right).left = curr; + } + cnt -= 1; + } + + if ptr::eq(curr, tail) { + break; + } + curr = (*curr).right; + } + } + } + head +} + +fn main() { + let mut line = String::new(); + stdin().read_line(&mut line).unwrap(); + + let n: usize = line.trim().parse().unwrap(); + + line.clear(); + stdin().read_line(&mut line).unwrap(); + + let a: Vec = line + .split(' ') + .map(|x| x.trim().parse::().unwrap()) + .collect(); + + let mut ptrs: Vec<*mut Cell> = Vec::new(); + let head = Cell::new(a[0], 0); + let tail = Cell::new(a[n - 1], n - 1); + ptrs.push(head); + let mut curr = head; + + for (i, v) in a.into_iter().enumerate().skip(1).take(n - 2) { + let cell = Cell::new(v, i); + unsafe { + (*cell).left = curr; + (*curr).right = cell; + curr = cell; + } + ptrs.push(cell); + } + unsafe { + (*tail).left = curr; + (*curr).right = tail; + } + + ptrs.push(tail); + + let last = last_man_standing(head, tail); + unsafe { + println!("{} {}", (*last).value, (*last).index + 1); + } + + unsafe { + for ele in ptrs { + Box::from_raw(ele); + } + } +}