CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:wolruso
Submission time:2024-11-04 20:36:47 +0200
Language:Rust (2021)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2details
#2ACCEPTED0.00 s1, 2details
#3ACCEPTED0.00 s1, 2details
#4ACCEPTED0.00 s1, 2details
#50.00 s1, 2details
#6ACCEPTED0.00 s1, 2details
#7ACCEPTED0.00 s1, 2details
#8ACCEPTED0.00 s1, 2details
#9ACCEPTED0.00 s1, 2details
#100.00 s1, 2details
#11ACCEPTED0.00 s1, 2details
#12ACCEPTED0.00 s2details
#13ACCEPTED0.00 s2details
#14ACCEPTED0.00 s2details
#150.00 s2details
#16ACCEPTED0.00 s2details
#17ACCEPTED0.00 s2details
#18ACCEPTED0.01 s2details
#19ACCEPTED0.01 s2details
#20ACCEPTED0.02 s2details
#210.00 s2details
#220.00 s2details
#230.02 s2details
#24ACCEPTED0.02 s2details

Code

use core::panic;
use std::alloc::{alloc, dealloc, handle_alloc_error, Layout};
use std::io::stdin;
use std::ptr::null_mut;

#[derive(PartialEq, Eq)]
enum Room {
    Empty,
    HasCoin,
}

pub struct LinkedListNode<T> {
    pub next: *mut LinkedListNode<T>,
    pub prev: *mut LinkedListNode<T>,
    pub data: T,
}

impl<T> LinkedListNode<T> {
    pub fn for_each<F: FnMut(&LinkedListNode<T>)>(&self, mut f: F) {
        let mut coin_iter = self as *const LinkedListNode<T>;
        while !coin_iter.is_null() {
            f(unsafe { &*coin_iter });
            coin_iter = unsafe { (*coin_iter).next };
        }
    }
    /// Removes the element the iterator is currently pointing at, and advances the iterator
    /// forward to point to the next element instead
    pub fn remove_this_element(&mut self) {
        unsafe {
            if !self.prev.is_null() {
                (*self.prev).next = self.next;
            }
            if !self.next.is_null() {
                (*self.next).prev = self.prev;
            }
            dealloc(
                self as *mut LinkedListNode<T> as *mut u8,
                Layout::new::<*mut LinkedListNode<T>>(),
            );
        }
    }
}

pub struct LinkedListIter<T> {
    address: *mut LinkedListNode<T>,
    first_node: *mut LinkedListNode<T>,
}

impl<T> Iterator for LinkedListIter<T> {
    type Item = *mut LinkedListNode<T>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.address.is_null() {
            self.address = self.first_node;
            None
        } else {
            let v = Some(self.address);
            self.address = unsafe { (*self.address).next };
            v
        }
    }
}

impl<T> LinkedListIter<T> {
    pub fn new(first_node: *mut LinkedListNode<T>) -> LinkedListIter<T> {
        LinkedListIter {
            address: first_node,
            first_node,
        }
    }
    pub fn new_with_node_address(
        first_node: *mut LinkedListNode<T>,
        address: *mut LinkedListNode<T>,
    ) -> LinkedListIter<T> {
        LinkedListIter {
            address,
            first_node,
        }
    }
    pub fn go_to_address(&mut self, address: *mut LinkedListNode<T>) {
        self.address = address;
    }
}

fn main() {
    let mut n = String::new();
    stdin().read_line(&mut n).unwrap();
    let _room_count = n.trim().parse::<i32>().unwrap();
    let mut room_map = String::new();
    stdin().read_line(&mut room_map).unwrap();
    let mut robot_position: isize = -1;
    let rooms = room_map
        .trim()
        .chars()
        .enumerate()
        .map(|(index, room_desc)| match room_desc {
            '*' => Room::HasCoin,
            '.' => Room::Empty,
            'R' => {
                robot_position = index as isize;
                Room::Empty
            }
            _ => panic!(),
        });

    let mut coins = Vec::new();
    for (index, room) in rooms.enumerate() {
        if room == Room::HasCoin {
            coins.push(index as isize);
        }
    }
    coins.sort_unstable();

    let mut first_coin: *mut LinkedListNode<isize> = null_mut();
    let mut current_coin: *mut LinkedListNode<isize> = null_mut();
    let mut prev_coin: *mut LinkedListNode<isize> = null_mut();
    let mut is_first_coin = true;
    for coin in coins {
        current_coin =
            unsafe { alloc(Layout::new::<LinkedListNode<isize>>()) } as *mut LinkedListNode<isize>;
        if current_coin.is_null() {
            handle_alloc_error(Layout::new::<LinkedListNode<isize>>());
        }
        if is_first_coin {
            first_coin = current_coin;
            unsafe {
                (*first_coin).prev = null_mut() as *mut LinkedListNode<isize>;
            }
            is_first_coin = false;
        } else {
            unsafe {
                (*prev_coin).next = current_coin;
                (*current_coin).prev = prev_coin;
            }
        }
        unsafe {
            (*current_coin).data = coin;
            (*current_coin).next = null_mut();
        }
        prev_coin = current_coin;
    }

    if robot_position == -1 {
        panic!()
    }
    let mut num_coins_collected = 0;
    let mut num_steps_taken = 0;
    let mut coin_iter = LinkedListIter::new(first_coin);
    for coin in &mut coin_iter {
        if unsafe { (*coin).data } >= robot_position {
            if unsafe { (*coin).prev }.is_null() {
                coin_iter.go_to_address(coin);
            } else {
                coin_iter.go_to_address(unsafe { (*coin).prev });
            }
            break;
        }
    }
    // There is no coin at a position more right than the robot
    if coin_iter.address == first_coin {
        coin_iter.go_to_address(current_coin);
    }
    loop {
        let (closest_coin, closest_coin_dis);

        let prev_coin = match coin_iter.next() {
            None => break,
            Some(c) => unsafe { &mut *c },
        };
        let disp = robot_position.abs_diff(prev_coin.data);
        match coin_iter.next() {
            None => {
                closest_coin = prev_coin;
                closest_coin_dis = disp;
            }
            Some(c) => {
                let c = unsafe { &mut *c };
                let disc = robot_position.abs_diff(c.data);
                if disc < disp {
                    closest_coin = c;
                    closest_coin_dis = disc;
                } else if disc > disp {
                    closest_coin = prev_coin;
                    closest_coin_dis = disp;
                } else {
                    break;
                }
            }
        }

        robot_position = closest_coin.data;
        if closest_coin.prev.is_null() {
            coin_iter.go_to_address(closest_coin.next);
        } else {
            coin_iter.go_to_address(closest_coin.prev);
        }
        closest_coin.remove_this_element();

        num_coins_collected += 1;
        num_steps_taken += closest_coin_dis;
    }
    println!("{} {}", num_steps_taken, num_coins_collected);
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
1
R

correct output
0 0

user output
0 0

Test 2

Group: 1, 2

Verdict: ACCEPTED

input
10
...R......

correct output
0 0

user output
0 0

Test 3

Group: 1, 2

Verdict: ACCEPTED

input
10
**.R...***

correct output
12 5

user output
12 5

Test 4

Group: 1, 2

Verdict: ACCEPTED

input
10
***R******

correct output
0 0

user output
0 0

Test 5

Group: 1, 2

Verdict:

input
1000
R................................

correct output
947 9

user output
1746 9

Test 6

Group: 1, 2

Verdict: ACCEPTED

input
1000
.................................

correct output
886 9

user output
886 9

Test 7

Group: 1, 2

Verdict: ACCEPTED

input
1000
.....*..*....**..**..*......*....

correct output
1287 400

user output
1287 400

Test 8

Group: 1, 2

Verdict: ACCEPTED

input
1000
************.*****************...

correct output
0 0

user output
0 0

Test 9

Group: 1, 2

Verdict: ACCEPTED

input
1000
******************************...

correct output
0 0

user output
0 0

Test 10

Group: 1, 2

Verdict:

input
1000
R*****************************...

correct output
999 999

user output
1997 999

Test 11

Group: 1, 2

Verdict: ACCEPTED

input
1000
******************************...

correct output
999 999

user output
999 999

Test 12

Group: 2

Verdict: ACCEPTED

input
10000
.......**........*...........*...

correct output
10971 999

user output
10971 999

Test 13

Group: 2

Verdict: ACCEPTED

input
10000
*..*....*......*.....*..*........

correct output
9999 999

user output
9999 999

Test 14

Group: 2

Verdict: ACCEPTED

input
10000
*.*.*...**.*...*....**.**.**.....

correct output
18766 5000

user output
18766 5000

Test 15

Group: 2

Verdict:

input
10000
R*****************************...

correct output
9999 9999

user output
19997 9999

Test 16

Group: 2

Verdict: ACCEPTED

input
10000
******************************...

correct output
9999 9999

user output
9999 9999

Test 17

Group: 2

Verdict: ACCEPTED

input
200000
.................................

correct output
0 0

user output
0 0

Test 18

Group: 2

Verdict: ACCEPTED

input
200000
.................................

correct output
299934 10000

user output
299934 10000

Test 19

Group: 2

Verdict: ACCEPTED

input
200000
**.***....**..**.....***.*..*....

correct output
299998 100000

user output
299998 100000

Test 20

Group: 2

Verdict: ACCEPTED

input
200000
******************************...

correct output
0 0

user output
0 0

Test 21

Group: 2

Verdict:

input
200000
R................................

correct output
133765 3

user output
197388 3

Test 22

Group: 2

Verdict:

input
200000
R................................

correct output
199982 5000

user output
399898 5000

Test 23

Group: 2

Verdict:

input
200000
R*****************************...

correct output
199999 199999

user output
399997 199999

Test 24

Group: 2

Verdict: ACCEPTED

input
200000
******************************...

correct output
199999 199999

user output
199999 199999