CSES - Datatähti 2024 alku - Results
Submission details
Task:Käännöt
Sender:drvilepis
Submission time:2023-11-02 12:12:20 +0200
Language:C++ (C++20)
Status:COMPILE ERROR

Compiler report

input/code.cpp:49:6: error: extended character  is not valid in an identifier
   49 | 2024  cat 5.cpp
      |      ^
input/code.cpp: In function 'int main()':
input/code.cpp:48:12: error: 'from' was not declared in this scope; did you mean 'fromfp'?
   48 | [detached (from session 2024)]
      |            ^~~~
      |            fromfp
input/code.cpp: In lambda function:
input/code.cpp:49:1: error: expected '{' before numeric constant
   49 | 2024  cat 5.cpp
      | ^~~~
input/code.cpp: In function 'int main()':
input/code.cpp:48:31: error: expected ';' before numeric constant
   48 | [detached (from session 2024)]
      |                               ^
      |                               ;
   49 | 2024  cat 5.cpp
      | ~~~~                           
input/code.cpp:63:18: error: a function-definition is not allowed here before '{' token
   63 | int scale(int n) {
      |                  ^
input/code.cpp:67:30: error: a function-definition is not allowed here before...

Code

#include <algorithm>
#include <array>
#include <iostream>
#include <valarray>
#include <vector>

const std::vector<std::vector<int>> reverses {
    std::vector<int> {1, -1},
    std::vector<int> {2, 0, -2},
    std::vector<int> {3, 1, -1, -3},
    std::vector<int> {4, 2, 0, -2, -4},
};

int scale(int n) {
    return n*(n+1)/2;
}

int distance(int n, int idx) {
    return idx-n;
}

int main() {
    int n;
    int k;


    std::cin >> n >> k;
    std::vector<int> reverse_offsets = reverses[k-2];

    std::vector<long> numbers(n);
    std::vector<long> distances(n);

    for (int i = 0; i < n; ++i) {
        std::cin >> numbers[i];
        numbers[i] -= 1;

        distances[i] = numbers[i] - i;
    }

    int min_move_dist = k-1;

    while (min_move_dist > 2) {
        min_move_dist -= 2;
    }


    for (int i = 0; i < n; ++i) {
[detached (from session 2024)]
2024  cat 5.cpp
#include <algorithm>
#include <array>
#include <iostream>
#include <valarray>
#include <vector>

const std::vector<std::vector<int>> reverses {
    std::vector<int> {1, -1},
    std::vector<int> {2, 0, -2},
    std::vector<int> {3, 1, -1, -3},
    std::vector<int> {4, 2, 0, -2, -4},
};

int scale(int n) {
    return n*(n+1)/2;
}

int distance(int n, int idx) {
    return idx-n;
}

int main() {
    int n;
    int k;


    std::cin >> n >> k;
    std::vector<int> reverse_offsets = reverses[k-2];

    std::vector<long> numbers(n);
    std::vector<long> distances(n);

    for (int i = 0; i < n; ++i) {
        std::cin >> numbers[i];
        numbers[i] -= 1;

        distances[i] = numbers[i] - i;
    }

    int min_move_dist = k-1;

    while (min_move_dist > 2) {
        min_move_dist -= 2;
    }


    for (int i = 0; i < n; ++i) {
        if (distances[i] % min_move_dist != 0) {
            std::cout << "NO" << std::endl;
            return 0;
        }
    }

    std::vector<int> ops;
    bool loop = true;

    std::vector<int> deltas(n-(k-1), 0);

    while (loop) {
        for (int i = 0; i < n-(k-1); ++i) {
            int delta = 0;

            for (int offset = 0; offset < k; ++offset) {
                int dist = distance(numbers[i+offset], i+offset);
                int dist_after = distance(numbers[i+offset], i+(k-1)-offset);

                delta  += scale(std::abs(dist)) - scale(std::abs(dist_after));
            }

            deltas[i] = delta;

        }

        if (!std::any_of(deltas.begin(), deltas.end(), [](int a){return a > 0;})) {
            break;
        }

        int swap = std::distance(deltas.begin(), std::max_element(deltas.begin(), deltas.end()));

        for (int offset = 0; offset < k/2; ++offset) {
            std::swap(numbers[swap+offset], numbers[swap+(k-1)-offset]);
        }

        ops.push_back(swap+1);
    }

    if (std::is_sorted(numbers.begin(), numbers.end())) {
        std::cout << "YES\n";
        std::cout << ops.size() << std::endl;
        for (auto op : ops) {
            std::cout << op << ' ';
        }
    } else {
        std::cout << "NO\n";
    }
}