CSES - Datatähti 2025 alku - Results
Submission details
Task:Robotti
Sender:Interaalimato
Submission time:2024-10-28 12:48:57 +0200
Language:C++ (C++20)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:30:5: error: 'stack' was not declared in this scope
   30 |     stack<int> leftPos;
      |     ^~~~~
input/code.cpp:3:1: note: 'std::stack' is defined in header '<stack>'; did you forget to '#include <stack>'?
    2 | #include <cmath>
  +++ |+#include <stack>
    3 | 
input/code.cpp:30:11: error: expected primary-expression before 'int'
   30 |     stack<int> leftPos;
      |           ^~~
input/code.cpp:31:5: error: 'queue' was not declared in this scope
   31 |     queue<int> rightPos;
      |     ^~~~~
input/code.cpp:3:1: note: 'std::queue' is defined in header '<queue>'; did you forget to '#include <queue>'?
    2 | #include <cmath>
  +++ |+#include <queue>
    3 | 
input/code.cpp:31:11: error: expected primary-expression before 'int'
   31 |     queue<int> rightPos;
      |           ^~~
input/code.cpp:42:17: error: 'rightPos' was not declared in this scope
   42 |                 rightPos.push(i);
      |                 ^...

Code

#include <iostream>
#include <cmath>

using namespace std;

// vector<tuple<string, int, int>> testcases = {
//     make_tuple("**.*......*.R*...*..", 4, 2),
//     make_tuple("***.R****", 12, 7),
//     make_tuple("****R.***", 12, 7),
//     make_tuple("R**********", 10, 10),
//     make_tuple("*************************************R****", 0, 0),
//     make_tuple("*.*.*R..*", 13, 4),
//     make_tuple("*.*..R***", 11, 5),
//     make_tuple("**...*R..*", 1, 1),
//     make_tuple("*...R**.....*", 2, 2),
// };

int main()
{
    // for (const auto &t : testcases)
    // {
    // string huoneet = get<0>(t);
    int n;
    string huoneet;
    cin >> n >> huoneet;
    // int n = huoneet.length();
    int kolikot = 0;
    int askeleet = 0;
    int pos = -1;
    stack<int> leftPos;
    queue<int> rightPos;
    for (int i = 0; i < n; i++)
    {
        if (huoneet.at(i) == 'R')
        {
            pos = i;
        }
        else if (huoneet.at(i) == '*')
        {
            if (pos != -1)
            {
                rightPos.push(i);
            }
            else
            {
                leftPos.push(i);
            }
        }
    }
    while (!leftPos.empty() || !rightPos.empty())
    {
        if (!leftPos.empty() && !rightPos.empty() && pos - leftPos.top() == rightPos.front() - pos)
        {
            break;
        }
        else if (rightPos.empty() || (!leftPos.empty() && pos - leftPos.top() < rightPos.front() - pos))
        {
            // Left is closer
            kolikot++;
            askeleet += (pos - leftPos.top());
            pos = leftPos.top();
            leftPos.pop();
        }
        else
        {
            // Right is closer
            kolikot++;
            askeleet += (rightPos.front() - pos);
            pos = rightPos.front();
            rightPos.pop();
        }
    }
    cout << askeleet << " " << kolikot << "\n";
    // }
    return 0;
}