CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:yo_chico1
Submission time:2024-11-03 21:38:24 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int getNear(const std::vector<int>&, int)':
input/code.cpp:10:33: error: 'INT_MAX' was not declared in this scope
   10 |     int closest = -1, minDiff = INT_MAX, count = 0;
      |                                 ^~~~~~~
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:19:21: error: overloaded function with no contextual type information
   19 |             count = 1;
      |                     ^
input/code.cpp:21:18: error: no post-increment operator for type
   21 |             count++;
      |                  ^~
input/code.cpp:22:23: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator>'
   22 |             if (count > 1) {
      |                 ~~~~~~^~~

Code

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

using namespace std;

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

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

        if (diff < minDiff) {
            minDiff = diff;
            closest = vec[mid];
            count = 1;
        } else if (diff == minDiff) {
            count++;
            if (count > 1) {
                return -1; 
            }
        }

        if (vec[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    return closest;
}

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

    cin >> mapSize;

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

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

    sort(moneyVec.begin(), moneyVec.end());

    while (!moneyVec.empty()) {
        int oldPos = myPos;
        myPos = getNear(moneyVec, myPos);

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

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

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

    cout << steps << ' ' << allc << endl;
    return 0;
}