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

Compiler report

input/code.cpp:6:10: fatal error: dbgHelper.h: No such file or directory
    6 | #include "dbgHelper.h"
      |          ^~~~~~~~~~~~~
compilation terminated.

Code

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

#include "dbgHelper.h"

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;
    PAUSE;
    return 0;
}