CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:osmukka
Submission time:2024-11-04 13:10:15 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:37:9: error: 'middle' was not declared in this scope
   37 |         middle = coin_rooms[robot_index];
      |         ^~~~~~

Code

#include <iostream>
#include <vector>
#include <string>

int main()
{
    int amount;
    char rooms[50000];
    int robot_index;
    int steps = 0;
    int coins = 0;

    std::cin >> amount;
    std::cin >> rooms;

    std::vector<int> coin_rooms;
    for(int i = 0; i < amount; i++)
    {
        switch(rooms[i])
        {
            case '*':
                coin_rooms.push_back(i);
                break;
            case 'R':
                coin_rooms.push_back(i);
                robot_index = coin_rooms.size()-1;
                break;
            default:
                break;
        }
    }
    
    int d_left;
    int d_right;
    for(int i = 0; i < amount; i++)
    {
        middle = coin_rooms[robot_index];
        d_left = robot_index > 0 ? coin_rooms[robot_index]-coin_rooms[robot_index-1] : 999999;
        d_right = robot_index < coin_rooms[coin_rooms.size()-1] ? coin_rooms[robot_index+1]-coin_rooms[robot_index] : 999999;
        
        if(d_left < d_right)
        {
            steps += d_left;
            coin_rooms.erase(coin_rooms.begin()+robot_index);
            robot_index--;
        }
        else if(d_right < d_left)
        {
            steps += d_right;
            coin_rooms.erase(coin_rooms.begin()+robot_index);
        }
        else if(d_left == d_right)
        {
            break;
        }
        coins++;
    }
    
    std::cout << steps << ' ' << coins << "\n";
    
    return 0;
    
}