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