| Task: | Enumeration |
| Sender: | Naming is (NP) Hard |
| Submission time: | 2025-11-08 16:37:32 +0200 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.00 s | details |
| #3 | ACCEPTED | 0.00 s | details |
| #4 | ACCEPTED | 0.00 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.00 s | details |
| #7 | ACCEPTED | 0.00 s | details |
| #8 | ACCEPTED | 0.01 s | details |
| #9 | ACCEPTED | 0.01 s | details |
| #10 | ACCEPTED | 0.03 s | details |
Code
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define D(x) {x;}
#else
#define D(x)
#endif
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x),end(x)
#define sz(x) (int) (x).size()
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
int n;
string buf;
set<string> visited;
void act(int a, int b) {
cout << a+1 << " " << b+1 << endl;
assert(buf[a] != buf[b]);
swap(buf[a], buf[b]);
D(cout << buf << endl);
D(
if (visited.count(buf)) {
assert(false);
}
visited.insert(buf);
);
}
bool valid(int x, int h) {
if (h < 0) return false;
if (h > x) return false;
if (h > n-x) return false;
return true;
}
void f(bool up, int lock, int h) {
if (h == 0) {
lock++;
h++;
}
if (lock+1 >= n) return;
int sum = 0;
for (int i = 0; i < lock; ++i) {
if (buf[i] == '(') sum++;
else sum--;
}
assert(sum == h);
D(cout << "ENTER f" << up << " " << lock << " " << h << endl);
if (up) {
// PRECONDITION: suffix is down
// for (int i = lock; i < n; ++i) assert(buf[i] == (i%2 ? ')' : '('));
} else {
// PRECONDITION: suffix is up
for (int i = lock; i < n-1; ++i) assert(!(buf[i] == ')' && buf[i+1] == '('));
}
if (up) {
int nlock = lock+1;
int nh = h-1;
int nup = up;
while (valid(nlock, nh) && nh < n-nlock) {
f(nup, nlock, nh);
int a = nlock-1;
int b = a;
while (b < n && buf[b] == ')') b++;
assert(b < n);
D(cout << "of " << up << " " << lock << " " << h << ": " << nlock << " " << nh << " " << nup << endl;)
act(a, b);
nup ^= 1;
nlock++;
nh++;
}
} else {
int nlock = lock;
int nh = h;
int nup = up;
while (valid(nlock+1, nh+1)) nlock++, nh++;
// PARITY CHECK
int steps = nlock - lock;
int par = steps%2;
if (!par) nup = !nup;
D(cout << "steps " << steps << " " << par << " " << nup << endl;)
while (nlock > lock) {
D(cout << "dw " << nlock << " " << nh << " " << nup << endl;)
if (!nup) {
act(nlock-1, nlock);
} else {
int a = nlock-1;
// XXX
int b = a+1;
while (b < n && buf[b] == ')') b++;
b -= 2;
// if (a >= b) break;
act(a, b);
}
f(nup, nlock, nh-2);
nup ^= 1;
nlock--;
nh--;
}
}
D(cout << "LEAVE f" << up << " " << lock << " " << h << endl);
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
cin.exceptions(cin.failbit);
cin >> n;
buf = string(n, 0);
for (int i = 0; i < n; ++i) buf[i] = i%2 ? ')' : '(';
visited.insert(buf);
cout << buf << endl;
f(1, 0, 0);
}
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 2 |
| correct output |
|---|
| () |
| user output |
|---|
| () |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 4 |
| correct output |
|---|
| ()() 2 3 |
| user output |
|---|
| ()() 2 3 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 6 |
| correct output |
|---|
| ()()() 2 3 4 5 3 2 2 4 |
| user output |
|---|
| ()()() 4 5 2 3 4 5 3 5 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 8 |
| correct output |
|---|
| ()()()() 2 3 4 5 3 2 2 4 ... |
| user output |
|---|
| ()()()() 6 7 4 5 6 7 5 7 ... |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 10 |
| correct output |
|---|
| ()()()()() 2 3 4 5 3 2 2 4 ... |
| user output |
|---|
| ()()()()() 8 9 6 7 8 9 7 9 ... |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 12 |
| correct output |
|---|
| ()()()()()() 2 3 4 5 3 2 2 4 ... |
| user output |
|---|
| ()()()()()() 10 11 8 9 10 11 9 11 ... |
Test 7
Verdict: ACCEPTED
| input |
|---|
| 14 |
| correct output |
|---|
| ()()()()()()() 2 3 4 5 3 2 2 4 ... |
| user output |
|---|
| ()()()()()()() 12 13 10 11 12 13 11 13 ... |
Test 8
Verdict: ACCEPTED
| input |
|---|
| 16 |
| correct output |
|---|
| ()()()()()()()() 2 3 4 5 3 2 2 4 ... |
| user output |
|---|
| ()()()()()()()() 14 15 12 13 14 15 13 15 ... |
Test 9
Verdict: ACCEPTED
| input |
|---|
| 18 |
| correct output |
|---|
| ()()()()()()()()() 2 3 4 5 3 2 2 4 ... |
| user output |
|---|
| ()()()()()()()()() 16 17 14 15 16 17 15 17 ... |
Test 10
Verdict: ACCEPTED
| input |
|---|
| 20 |
| correct output |
|---|
| ()()()()()()()()()() 2 3 4 5 3 2 2 4 ... |
| user output |
|---|
| ()()()()()()()()()() 18 19 16 17 18 19 17 19 ... |
