CSES - Aalto Competitive Programming 2024 - wk4 - Wed - Results
Submission details
Task:Bathhouse
Sender:aalto2024d_002
Submission time:2024-09-25 17:31:27 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'std::vector<long long int> find_maija_positions(int, int, const std::vector<int>&, const std::vector<long long int>&)':
input/code.cpp:13:20: error: 'max_element' was not declared in this scope
   13 |     while (time < *max_element(queries.begin(), queries.end())) {
      |                    ^~~~~~~~~~~
input/code.cpp:22:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |                 if (a < positions.size()) {
      |                     ~~^~~~~~~~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

vector<long long> find_maija_positions(int n, int q, const vector<int>& flows, const vector<long long>& queries) {
    vector<int> positions;
    unordered_map<int, int> visited;
    int current = 1;
    int time = 0;

    while (time < *max_element(queries.begin(), queries.end())) {
        if (visited.find(current) != visited.end()) {
            int cycle_start = visited[current];
            int cycle_length = time - cycle_start;

            vector<int> cycle_positions(positions.begin() + cycle_start, positions.end());
            vector<long long> results;

            for (long long a : queries) {
                if (a < positions.size()) {
                    results.push_back(positions[a]);
                } else {
                    long long effective_time = (a - cycle_start) % cycle_length + cycle_start;
                    results.push_back(positions[effective_time]);
                }
            }
            return results;
        }
        visited[current] = time;
        positions.push_back(current);
        current = flows[current - 1];
        time++;
    }

    vector<long long> results;
    for (long long a : queries) {
        results.push_back(positions[min(a, (long long)positions.size() - 1)]);
    }

    return results;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;
    vector<int> flows(n);
    vector<long long> queries(q);

    for (int i = 0; i < n; ++i) {
        cin >> flows[i];
    }
    for (int i = 0; i < q; ++i) {
        cin >> queries[i];
    }

    vector<long long> results = find_maija_positions(n, q, flows, queries);
    for (long long res : results) {
        cout << res << " ";
    }
    cout << "\n";

    return 0;
}