CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:yo_chico1
Submission time:2024-10-31 20:22:41 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:29:28: error: 'INT_MAX' was not declared in this scope
   29 |         int min_distance = INT_MAX;
      |                            ^~~~~~~
input/code.cpp:4:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    3 | #include <cmath>
  +++ |+#include <climits>
    4 | 
input/code.cpp:31:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         for (int i = 0; i < coin_positions.size(); ++i) {
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main() {
    int n;
    string map;

    cin >> n;
    cin >> map;

    vector<int> coin_positions;
    int robot_pos;

    for (int i = 0; i < n; ++i) {
        if (map[i] == '*') {
            coin_positions.push_back(i);
        } else if (map[i] == 'R') {
            robot_pos = i;
        }
    }

    int steps = 0, coins = 0;

    while (!coin_positions.empty()) {
        int closest_coin_index = 0;
        int min_distance = INT_MAX;

        for (int i = 0; i < coin_positions.size(); ++i) {
            int distance = abs(coin_positions[i] - robot_pos);
            if (distance < min_distance) {
                min_distance = distance;
                closest_coin_index = i;
            } else if (distance == min_distance) {
                cout << steps << " " << coins << endl;
                return 0;
            }
        }

        int new_pos = coin_positions[closest_coin_index];
        steps += abs(new_pos - robot_pos);
        coins++;
        robot_pos = new_pos;
        coin_positions.erase(coin_positions.begin() + closest_coin_index);
    }

    cout << steps << " " << coins << endl;
    return 0;
}