QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#444213 | #8523. Puzzle II | ucup-team133# | WA | 0ms | 3852kb | C++23 | 4.1kb | 2024-06-15 17:48:48 | 2024-06-15 17:48:49 |
Judging History
answer
#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif
template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) {
for (auto& e : v) {
is >> e;
}
return is;
}
template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
for (std::string_view sep = ""; const auto& e : v) {
os << std::exchange(sep, " ") << e;
}
return os;
}
template <class T, class U = T> bool chmin(T& x, U&& y) {
return y < x and (x = std::forward<U>(y), true);
}
template <class T, class U = T> bool chmax(T& x, U&& y) {
return x < y and (x = std::forward<U>(y), true);
}
template <class T> void mkuni(std::vector<T>& v) {
std::ranges::sort(v);
auto result = std::ranges::unique(v);
v.erase(result.begin(), result.end());
}
template <class T> int lwb(const std::vector<T>& v, const T& x) {
return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}
using ll = long long;
using namespace std;
struct viewer {
deque<int> main, other;
int l, r;
void push_l() {
main.emplace_front(other.back());
other.pop_back();
l--;
}
void push_r() {
main.emplace_back(other.front());
other.pop_front();
r++;
}
void pop_l() {
other.emplace_back(main.front());
main.pop_front();
l++;
}
void pop_r() {
other.emplace_front(main.back());
main.pop_back();
r--;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int n, k;
cin >> n >> k;
string S, T;
cin >> S >> T;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
a[i] = (S[i] == 'C');
b[i] = (T[i] == 'C');
}
// a -> all 0
{
int one = accumulate(a.begin(), a.end(), 0);
if (one > n - one) {
for (int& val : a) val ^= 1;
for (int& val : b) val ^= 1;
}
}
vector<int> ca = a, cb = b;
vector<pair<int, int>> ans;
auto f = [&](int x) { return (x + n) % n; };
auto op = [&](int c, int d) {
assert(0 <= c and c < n);
assert(0 <= d and d < n);
ans.emplace_back(c, d);
#ifdef LOCAL
for (int i = 0; i < k; i++) {
swap(ca[f(c + i)], cb[f(d + i)]);
}
#endif
};
if (k == 1) {
vector<int> P, Q;
for (int i = 0; i < n; i++) {
if (a[i] == 1) P.emplace_back(i);
if (b[i] == 0) Q.emplace_back(i);
}
for (int i = 0; i < int(P.size()); i++) op(P[i], Q[i]);
} else {
viewer A, B;
A.l = n - (k + 1), A.r = n;
B.l = 0, B.r = k;
auto SWAP = [&]() { // swap a[p] and b[q]
int p = f(A.r - 1), q = f(B.l);
op(f(p - k), f(q));
op(f(p - k + 1), f(q));
int tmp = A.main.back();
A.main.pop_back();
A.main.emplace_front(B.main.front());
B.main.pop_front();
int tmp2 = B.main.back();
B.main.pop_back();
B.main.emplace_back(tmp);
B.main.emplace_back(tmp2);
};
for (int i = A.l; i < A.r; i++) A.main.emplace_back(a[i]);
for (int i = 0; i < n - (k + 1); i++) A.other.emplace_back(a[f(A.r + i)]);
for (int i = B.l; i < B.r; i++) B.main.emplace_back(b[i]);
for (int i = 0; i < n - k; i++) B.other.emplace_back(b[f(B.r + i)]);
while (true) {
for (int i = 0; i < n; i++) {
if (A.main.back() == 1) break;
A.pop_r();
A.push_l();
}
if (A.main.back() != 1) break;
for (int i = 0; i < n; i++) {
if (B.main.front() == 0) break;
B.pop_l();
B.push_r();
}
assert(B.main.front() == 0);
SWAP();
}
}
#ifdef LOCAL
for (int& val : ca) assert(val == 0);
for (int& val : cb) assert(val == 1);
#endif
cout << ans.size() << "\n";
for (auto [c, d] : ans) cout << c + 1 << " " << d + 1 << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3616kb
input:
6 3 BCCBCC BBCBBC
output:
4 1 3 2 3 5 6 6 6
result:
ok moves = 4
Test #2:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
2 1 BC BC
output:
1 2 1
result:
ok moves = 1
Test #3:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
2 1 BB CC
output:
0
result:
ok moves = 0
Test #4:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
2 1 CC BB
output:
0
result:
ok moves = 0
Test #5:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
3 1 CCC BBB
output:
0
result:
ok moves = 0
Test #6:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
3 1 CBC BCB
output:
1 2 2
result:
ok moves = 1
Test #7:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
3 2 BBB CCC
output:
0
result:
ok moves = 0
Test #8:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
3 2 BCB BCC
output:
2 3 1 1 1
result:
ok moves = 2
Test #9:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
4 2 CCCB BBCB
output:
2 2 3 3 3
result:
ok moves = 2
Test #10:
score: -100
Wrong Answer
time: 0ms
memory: 3584kb
input:
9 6 CCCBCCCBB BBBCBBBCC
output:
6 3 4 4 4 3 7 4 7 9 8 1 8
result:
wrong answer The final sequences are not correct!