CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:wolruso
Submission time:2024-11-04 17:57:02 +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
#60.00 s1, 2details
#7ACCEPTED0.00 s1, 2details
#8ACCEPTED0.00 s1, 2details
#9ACCEPTED0.00 s1, 2details
#100.00 s1, 2details
#110.00 s1, 2details
#12ACCEPTED0.00 s2details
#130.00 s2details
#14ACCEPTED0.00 s2details
#150.00 s2details
#160.00 s2details
#17ACCEPTED0.00 s2details
#18ACCEPTED0.01 s2details
#19ACCEPTED0.01 s2details
#20ACCEPTED0.01 s2details
#210.00 s2details
#220.00 s2details
#230.02 s2details
#240.01 s2details

Compiler report

warning: method `for_each` is never used
  --> input/code.rs:19:12
   |
18 | impl<T> LinkedListNode<T> {
   | ------------------------- method in this implementation
19 |     pub fn for_each<F: FnMut(&LinkedListNode<T>)>(&self, mut f: F) {
   |            ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: associated function `new_with_node_address` is never used
  --> input/code.rs:70:12
   |
63 | impl<T> LinkedListIter<T> {
   | ------------------------- associated function in this implementation
...
70 |     pub fn new_with_node_address(
   |            ^^^^^^^^^^^^^^^^^^^^^

warning: 2 warnings emitted

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,
}

struct LinkedListNode<T> {
    next: *mut LinkedListNode<T>,
    prev: *mut LinkedListNode<T>,
    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<usize>>(),
            );
        }
    }
}

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);
        }
    }
    coins.sort_unstable();

    let mut first_coin: *mut LinkedListNode<usize> = null_mut();
    let mut current_coin: *mut LinkedListNode<usize>;
    let mut prev_coin: *mut LinkedListNode<usize> = null_mut();
    let mut is_first_coin = true;
    for coin in coins {
        current_coin =
            unsafe { alloc(Layout::new::<LinkedListNode<usize>>()) } as *mut LinkedListNode<usize>;
        if current_coin.is_null() {
            handle_alloc_error(Layout::new::<LinkedListNode<usize>>());
        }
        if is_first_coin {
            first_coin = current_coin;
            unsafe {
                (*first_coin).prev = null_mut() as *mut LinkedListNode<usize>;
            }
            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 robot_position: usize = robot_position as usize;

    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 {
            coin_iter.go_to_address(unsafe { (*coin).prev });
            break;
        }
    }
    loop {
        let mut closest_coin: *mut LinkedListNode<usize> = null_mut();
        let mut closest_coin_dis: usize = 0;
        let mut should_exit: bool = false;
        let prev_coin = match coin_iter.next() {
            None => break,
            Some(c) => c,
        };
        let disp = (robot_position as isize).abs_diff(unsafe { (*prev_coin).data } as isize);
        match coin_iter.next() {
            None => {
                closest_coin = prev_coin;
                closest_coin_dis = disp;
            }
            Some(c) => {
                let disc = (robot_position as isize).abs_diff(unsafe { (*c).data } as isize);
                if disc < disp {
                    closest_coin = c;
                    closest_coin_dis = disc;
                } else if disc > disp {
                    closest_coin = prev_coin;
                    closest_coin_dis = disp;
                } else {
                    should_exit = true;
                }
            }
        }
        if should_exit || closest_coin.is_null() {
            break;
        }
        let closest_coin = unsafe { &mut *closest_coin };
        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
0 0

Test 6

Group: 1, 2

Verdict:

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

correct output
886 9

user output
1613 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
0 0

Test 11

Group: 1, 2

Verdict:

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

correct output
999 999

user output
998 1

Test 12

Group: 2

Verdict: ACCEPTED

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

correct output
10971 999

user output
10971 999

Test 13

Group: 2

Verdict:

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

correct output
9999 999

user output
19993 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
0 0

Test 16

Group: 2

Verdict:

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

correct output
9999 9999

user output
9998 1

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
0 0

Test 22

Group: 2

Verdict:

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

correct output
199982 5000

user output
0 0

Test 23

Group: 2

Verdict:

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

correct output
199999 199999

user output
0 0

Test 24

Group: 2

Verdict:

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

correct output
199999 199999

user output
199998 1