CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:yo_chico1
Submission time:2024-10-31 19:02:51 +0200
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int getNear(const std::vector<int>&, int)':
input/code.cpp:11:19: error: 'INT_MAX' was not declared in this scope
   11 |     int minDiff = INT_MAX;
      |                   ^~~~~~~
input/code.cpp:5:1: note: 'INT_MAX' is defined in header '<climits>'; did you forget to '#include <climits>'?
    4 | #include <algorithm>
  +++ |+#include <climits>
    5 | 
input/code.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |     for (int i = 0; i < vec.size(); ++i) {
      |                     ~~^~~~~~~~~~~~

Code

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


//#define PAUSE system("pause")

int getNear(const std::vector<int>& vec, int target) {
    int closest = -1;
    int minDiff = INT_MAX;

    for (int i = 0; i < vec.size(); ++i) {
        int currentDiff = std::abs(target - vec[i]);
        if (currentDiff < minDiff) {
            minDiff = currentDiff;
            closest = vec[i];
        } else if (currentDiff == minDiff) {
            closest = -1;
        }
    }
    return closest;
}

int main() {
    int mapSize, steps = 0, allc = 0;
    std::vector<char> mapVec;
    char el_buffer;

    std::cin >> mapSize;

    for (int i = 0; i < mapSize; i++) {
        std::cin >> el_buffer;
        mapVec.push_back(el_buffer);
    }

    std::vector<int> moneyVec;
    int myPos;

    for (int i = 0; i < mapSize; i++) {
        if (mapVec[i] == '*') {
            moneyVec.push_back(i);
        } else if (mapVec[i] == 'R') {
            myPos = i;
        }
    }

    while (true) {
        int oldPos = myPos;
        myPos = getNear(moneyVec, myPos);

        if (myPos == -1) {
            break;
        }

        mapVec[oldPos] = '.';
        mapVec[myPos] = 'R';
        allc++;
        steps += std::abs(myPos - oldPos);

        moneyVec.erase(std::remove(moneyVec.begin(), moneyVec.end(), myPos), moneyVec.end());
    }

    std::cout << steps << ' ' << allc << std::endl;
    //PAUSE;
    return 0;
}