CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:yo_chico1
Submission time:2024-10-31 20:03:25 +0200
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int getNear(const std::vector<int>&, int)':
input/code.cpp:12:19: error: 'INT_MAX' was not declared in this scope
   12 |     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 |

Code

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

//#define PAUSE system("pause")

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

    while (left <= right) {
        int mid = left + (right - left) / 2;
        int diff = abs(vec[mid] - target);

        if (diff < minDiff) {
            minDiff = diff;
            closest = vec[mid];
        }

        if (vec[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 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;
}