QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#444460 | #8523. Puzzle II | ucup-team3564# | WA | 1ms | 7936kb | C++20 | 4.0kb | 2024-06-15 19:21:35 | 2024-06-15 19:21:35 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\033[32m[" << __LINE__ << "]\033[0m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T& x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = - f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= f;
}
mt19937 mrand(20071211);//chrono::steady_clock::now().time_since_epoch().count());
// inline int rnd(int l, int r) {return mrand() % (r - l + 1) + l;}
const int N = 3e5 + 10;
int n, k, a[N], b[N];
int ls[N], rs[N], s[N][2], sz[N], rt1, rt2;
bool val[N];
unsigned key[N];
void pushup(int num) {
sz[num] = sz[ls[num]] + sz[rs[num]] + 1;
F(i, 0, 1) s[num][i] = s[ls[num]][i] + s[rs[num]][i] + (val[num] == i);
}
int merge(int num1, int num2) {
if (!num1 || !num2) return num1 ^ num2;
if (key[num1] < key[num2]) return rs[num1] = merge(rs[num1], num2), pushup(num1), num1;
return ls[num2] = merge(num1, ls[num2]), pushup(num2), num2;
}
pair <int, int> split(int num, int k) {
if (!num) return {0, 0};
if (k >= sz[ls[num]] + 1) {
auto [a, b] = split(rs[num], k - sz[ls[num]] - 1);
rs[num] = a;
pushup(num);
return make_pair(num, b);
} else {
auto [a, b] = split(ls[num], k);
ls[num] = b;
pushup(num);
return make_pair(a, num);
}
}
// int find1(int x) {
// if (sum[ls[x]]) return find1(ls[x]);
// if (val[x]) return sz[ls[x]] + 1;
// return sz[ls[x]] + 1 + find1(rs[x]);
// }
// int find0(int x) {
// if (sum[ls[x]] != sz[ls[x]]) return find0(ls[x]);
// if (!val[x]) return sz[ls[x]] + 1;
// return sz[ls[x]] + 1 + find0(rs[x]);
// }
int find(int x, bool y) {
// debug << x << endl;
if (s[ls[x]][y]) return find(ls[x], y);
if (val[x] == y) return sz[ls[x]] + 1;
return sz[ls[x]] + 1 + find(rs[x], y);
}
signed main() {
read(n), read(k);
F(i, 1, 2 * n) key[i] = mrand();
F(i, 1, n) {
char ch;
do {
ch = getchar();
} while (ch != 'B' && ch != 'C');
a[i] = ch == 'C';
s[i][val[i] = a[i]] = 1;
sz[i] = 1;
rt1 = merge(rt1, i);
}
F(i, 1, n) {
char ch;
do {
ch = getchar();
} while (ch != 'B' && ch != 'C');
b[i] = ch == 'C';
s[i + n][val[i + n] = b[i]] = 1;
sz[i + n] = 1;
rt2 = merge(rt2, i + n);
}
bool w = val[rt1] > val[rt2];
vector <pair <int, int>> ans;
int s1 = 0, s2 = 0;
// debug << w << endl;
while (s[rt1][!w]) {
// debug << s[rt1][!w] << " " << s[rt2][w] << endl;
int pos1 = find(rt1, !w), pos2 = find(rt2, w);
// debug << "% " << pos1 << " " << pos2 << endl;
pos2 = (pos2 - k - 1 + n) % n + 1;
if (pos1 > 1) {
s1 += pos1 - 1;
auto [a, b] = split(rt1, pos1 - 1);
rt1 = merge(b, a);
}
if (pos2 > 1) {
s2 += pos2 - 1;
auto [a, b] = split(rt2, pos2 - 1);
rt2 = merge(b, a);
}
// debug << "OK\n";
{
auto [a, b] = split(rt1, 1);
auto [c, d] = split(b, k - 1);
// debug << val[a] << endl;
// assert(val[a] != w);
s[a][val[a]]--;
s[a][val[a] ^= true]++;
rt1 = merge(c, merge(a, d));
}
// debug << "OK\n";
{
auto [a, b] = split(rt2, k);
auto [c, d] = split(b, 1);
// debug << val[c] << endl;
s[c][val[c]]--;
s[c][val[c] ^= true]++;
rt2 = merge(c, merge(a, d));
}
ans.emplace_back(s1 + 1, s2 + 1);
ans.emplace_back(s1 + 1, s2 + 2);
// int a1, b1, c1;
// int a2, b2, c2;
// {
// if (pos1 + k <= n) {
// auto [a, b] = split(rt1, pos1 - 1);
// auto [b, c] = split(b, k);
// }
// }
}
cout << ans.size() << '\n';
for (auto [a, b]: ans) cout << (a - 1) % n + 1 << ' ' << (b - 1) % n + 1 << '\n';
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5924kb
input:
6 3 BCCBCC BBCBBC
output:
4 1 6 1 1 4 4 4 5
result:
ok moves = 4
Test #2:
score: 0
Accepted
time: 0ms
memory: 7936kb
input:
2 1 BC BC
output:
2 2 2 2 1
result:
ok moves = 2
Test #3:
score: 0
Accepted
time: 1ms
memory: 5888kb
input:
2 1 BB CC
output:
0
result:
ok moves = 0
Test #4:
score: 0
Accepted
time: 1ms
memory: 5696kb
input:
2 1 CC BB
output:
0
result:
ok moves = 0
Test #5:
score: 0
Accepted
time: 1ms
memory: 5924kb
input:
3 1 CCC BBB
output:
0
result:
ok moves = 0
Test #6:
score: 0
Accepted
time: 1ms
memory: 5572kb
input:
3 1 CBC BCB
output:
2 2 1 2 2
result:
ok moves = 2
Test #7:
score: 0
Accepted
time: 1ms
memory: 7728kb
input:
3 2 BBB CCC
output:
0
result:
ok moves = 0
Test #8:
score: 0
Accepted
time: 0ms
memory: 5624kb
input:
3 2 BCB BCC
output:
2 2 2 2 3
result:
ok moves = 2
Test #9:
score: 0
Accepted
time: 0ms
memory: 5928kb
input:
4 2 CCCB BBCB
output:
2 4 1 4 2
result:
ok moves = 2
Test #10:
score: 0
Accepted
time: 1ms
memory: 5692kb
input:
9 6 CCCBCCCBB BBBCBBBCC
output:
6 4 7 4 8 7 3 7 4 7 4 7 5
result:
ok moves = 6
Test #11:
score: 0
Accepted
time: 1ms
memory: 5724kb
input:
21 3 CCCCBBCBCCCCCCCBCCCCC BBCCBCBBBBBBBBBCBBBBB
output:
8 5 21 5 1 5 1 5 2 8 3 8 4 16 13 16 14
result:
ok moves = 8
Test #12:
score: -100
Wrong Answer
time: 1ms
memory: 7768kb
input:
49 41 CBCCBCCBCCBCCBCCCBBCCBCBBCCCBBCCBCBCBCBCCCCBCBCCB BCCCCBCBBBBCBCBBBBBCBBBBCCCCBCBBCBBCBBBBCBCBCBBBC
output:
60 1 9 1 10 2 18 2 19 2 27 2 28 3 40 3 41 3 49 3 1 4 10 4 11 4 19 4 20 5 29 5 30 5 38 5 39 6 47 6 48 6 7 6 8 6 16 6 17 8 26 8 27 8 35 8 36 9 44 9 45 11 4 11 5 11 13 11 14 11 22 11 23 13 33 13 34 13 42 13 43 14 10 14 11 15 19 15 20 16 37 16 38 17 46 17 47 17 14 17 15 18 24 18 25 20 42 20 43 25 2 25 3...
result:
wrong answer Integer 60 violates the range [0, 49]