QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#932061 | #9538. Closest Derangement | nhuang685 | WA | 1ms | 3584kb | C++23 | 5.4kb | 2025-03-12 07:07:06 | 2025-03-12 07:07:06 |
Judging History
answer
/**
* @author n685
* @date 2025-03-10 18:07:49
*/
#include <bits/stdc++.h>
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420
void nline() {}
void bar() {}
void start_clock() {}
void end_clock() {}
#endif
namespace rs = std::ranges;
namespace rv = std::views;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
template <class T> constexpr T INF = T{};
template <std::floating_point T>
constexpr T INF<T> = std::numeric_limits<T>::infinity();
template <> constexpr int INF<int> = 0x3f3f3f3f; // 1061109567
template <>
constexpr long long INF<long long>
= 0x3f3f3f3f3f3f3f3fLL; // 4557430888798830399
namespace seg {
template <class Node>
concept SegNode = requires(Node n) {
typename Node::Output;
Node();
requires std::
same_as<std::decay_t<decltype(Node::ID)>, typename Node::Output>;
{
Node::comb(Node::ID, Node::ID)
} -> std::same_as<typename Node::Output>;
{
std::as_const(n).value()
} -> std::same_as<typename Node::Output>;
{
n.pull(n, n)
} -> std::same_as<void>;
};
template <SegNode Node> struct Seg {
using Output = typename Node::Output;
int sz{};
std::vector<Node> val;
Seg() = default;
explicit Seg(int _sz) : sz(_sz), val(2 * sz) {}
// explicit Seg(int _sz)
// : sz(static_cast<int>(std::bit_ceil<uint32_t>(_sz))),
// val(2 * sz) {}
template <std::ranges::sized_range Container>
requires requires(Container c) { Node(*std::ranges::begin(c)); }
explicit Seg(const Container& c)
: Seg(static_cast<int>(std::ranges::size(c))) {
int i = sz;
for (const auto& v : c) {
val[i++] = Node(v);
}
build(0, sz - 1);
}
void pull(int ind) { val[ind].pull(val[2 * ind], val[2 * ind + 1]); }
void build(int l, int r) {
l += sz, r += sz;
l /= 2, r /= 2;
for (int k = 2; l >= 1; l /= 2, r /= 2, k *= 2) {
for (int i = l; i <= r; ++i) {
val[i].pull(val[2 * i], val[2 * i + 1]);
}
}
}
template <class F, class... Args>
requires std::invocable<F, Node&, const Args&...>
void upd(const F& f, int i, const Args&... args) {
std::invoke(f, val[i += sz], args...);
for (i /= 2; i >= 1; i /= 2) {
pull(i);
}
}
Output query(int l, int r) {
Output vl = Node::ID, vr = Node::ID;
for (l += sz, r += sz; l <= r; l /= 2, r /= 2) {
if (l % 2 == 1) {
vl = Node::comb(vl, val[l++].value());
}
if (r % 2 == 0) {
vr = Node::comb(val[r--].value(), vr);
}
}
return Node::comb(vl, vr);
}
};
} // namespace seg
using seg::Seg;
template <class T> struct Node {
using Output = T;
static constexpr Output ID{INF<T>};
T val{};
Node() = default;
explicit Node(T v) : val(v) {}
static Output comb(Output a, Output b) { return std::min(a, b); }
Output value() const { return val; }
void add(const T& v) { val += v; }
void set(const T& v) { val = v; }
void pull(const Node& cl, const Node& cr) { val = comb(cl.val, cr.val); }
};
struct Perm {
int x;
bool fl; // not fl: go to left: 1, 2, 3 -> 2, 3, 1
// fl: go to right: 1, 2, 3 -> 3, 1, 2
int transform(int v) const {
if (v < x) {
if (v % 2 == x % 2) {
return v + 1;
}
return v - 1;
}
if (v > x + 2) {
if (v % 2 == x % 2) {
return v - 1;
}
return v + 1;
}
if (!fl) {
if (v < x + 2) {
return v + 1;
}
return v - 2;
}
if (v > x) {
return v - 1;
}
return v + 2;
}
};
void tc() {
int n, k;
std::cin >> n >> k;
std::vector<int> p(n);
for (int& v : p) {
std::cin >> v;
--v;
}
if ((n % 2 == 0 && k > 1) || (n % 2 == 1 && k > 2 * (n - 2))) {
std::cout << "-1\n";
return;
}
std::vector<int> pos(n);
for (int i = 0; i < n; ++i) {
pos[p[i]] = i;
}
if (n % 2 == 0) {
for (int i = 0; i < n; i += 2) {
std::swap(p[pos[i]], p[pos[i + 1]]);
}
for (int i : p) {
std::cout << i + 1 << ' ';
}
std::cout << '\n';
return;
}
Seg<Node<int>> seg(pos);
std::vector<Perm> pe;
pe.reserve(2 * n - 2);
for (int i = 0; i < n - 2; ++i) {
pe.emplace_back(i, false);
pe.emplace_back(i, true);
}
rs::nth_element(
pe,
pe.begin() + k - 1,
[&](const Perm& p1, const Perm& p2) -> bool {
std::vector<int> inds{
p1.x,
p1.x + 1,
p1.x + 2,
p2.x,
p2.x + 1,
p2.x + 2,
seg.query(std::min(p1.x + 2, p2.x + 2) + 1, std::max(p1.x, p2.x) - 1)
};
rs::sort(inds);
inds.erase(rs::unique(inds).begin(), inds.end());
if (inds.back() == INF<int>) {
inds.pop_back();
}
for (int i : inds) {
int v1 = p1.transform(p[i]);
int v2 = p2.transform(p[i]);
if (v1 < v2) {
return true;
}
if (v1 > v2) {
return false;
}
}
return false;
}
);
Perm ans = pe[k - 1];
dbg(ans.x, ans.fl);
for (int i : p) {
std::cout << ans.transform(i) + 1 << ' ';
}
std::cout << '\n';
}
int main() {
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
int t;
std::cin >> t;
for (int i = 0; i < t; ++i) {
dbg(i + 1);
tc();
bar();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3456kb
input:
2 2 2 2 1 3 2 1 2 3
output:
-1 3 1 2
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3584kb
input:
50 6 1 4 2 6 3 5 1 8 1 6 2 8 7 3 4 1 5 3 298728530 2 3 1 4 610615077 2 4 3 1 4 2 4 2 3 1 7 152065238 4 1 3 5 7 6 2 6 844435075 2 6 4 5 1 3 4 544008000 3 2 4 1 9 379783780 5 9 3 8 4 2 1 7 6 5 139426002 2 4 5 3 1 2 1 1 2 2 1 1 2 3 1 3 1 2 4 2 2 4 1 3 4 1 3 2 4 1 5 4 2 4 1 5 3 3 1 1 2 3 6 805720317 5 6...
output:
3 1 5 4 6 2 5 1 7 8 4 3 2 6 -1 -1 -1 -1 -1 -1 -1 -1 2 1 2 1 1 2 3 -1 4 1 3 2 4 3 0 6 2 2 3 1 -1 -1 -1 2 1 -1 -1 2 4 3 1 -1 -1 -1 -1 -1 5 3 7 8 6 4 1 2 1 3 2 -1 3 5 7 2 10 4 6 0 8 -1 1 2 -1 -1 -1 8 5 3 2 4 7 1 6 -1 -1 2 3 8 5 4 6 0 -1 3 4 6 1 5 2 7 1 2 -1 2 1 -1 4 2 1 3 5 -1
result:
wrong answer 16th lines differ - expected: '3 5 2 4 1', found: '4 3 0 6 2 '