QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#376539 | #6545. Connect the Dots | Fido_Puppy | WA | 1ms | 5796kb | C++23 | 2.4kb | 2024-04-04 11:31:54 | 2024-04-04 11:31:56 |
Judging History
answer
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define MT make_tuple
#define IT iterator
#define fi first
#define se second
#define For(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define Rep(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define CLR(a, v) memset(a, v, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define debug cerr << "ztxakking\n"
#define y0 ztxaknoi
#define y1 ztxakioi
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using vi = vector<int>;
template<typename T>
using V = vector<T>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 7;
int n, m, a[N], pre[N], nxt[N], cnt[N];
void del(int u) {
nxt[pre[u]] = nxt[u], pre[nxt[u]] = pre[u];
}
set<int> s;
V<pii> ans;
void push(int x, int y) {
ans.eb(x, y);
}
void print() {
cout << ans.size() << '\n';
for (auto [x, y] : ans) cout << x << ' ' << y << '\n';
}
void sol() {
cin >> n >> m, ans.clear();
For(i, 1, m) cnt[i] = 0;
For(i, 1, n) cin >> a[i], ++cnt[a[i]];
For(i, 1, n - 1) if (a[i] != a[i + 1]) push(i, i + 1);
For(i, 2, n - 1) pre[i] = i - 1, nxt[i] = i + 1;
nxt[1] = 2, pre[n] = n - 1, s.clear();
For(i, 2, n - 1) if (a[pre[i]] != a[nxt[i]]) s.insert(i);
for (int t = n - 2; t; --t) {
if (s.size()) {
int u = *s.begin(); s.erase(s.begin());
if (cnt[a[u]] == 1) {
for (int i = pre[u]; i > 1; i = pre[i]) push(pre[i], nxt[i]), del(i);
for (int i = nxt[u]; i < n; i = nxt[i]) push(pre[i], nxt[i]), del(i);
if (a[1] != a[n]) push(1, n);
break;
} else {
push(pre[u], nxt[u]);
int x = pre[u], y = nxt[u];
del(u);
if (x > 1 && a[pre[x]] != a[nxt[x]]) s.insert(x);
else {
if (s.find(x) != s.end()) s.erase(x);
}
if (y < n && a[pre[y]] != a[nxt[y]]) s.insert(y);
else {
if (s.find(y) != s.end()) s.erase(y);
}
}
} else {
int u = nxt[1], v = nxt[u];
del(u);
if (v < n && a[1] != a[nxt[v]]) s.insert(v);
}
}
print();
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int t; cin >> t;
while (t--) sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5684kb
input:
3 4 2 1 1 2 2 4 2 1 2 1 2 3 3 1 2 3
output:
3 2 3 1 3 1 4 4 1 2 2 3 3 4 1 4 3 1 2 2 3 1 3
result:
ok all 3 test passed
Test #2:
score: 0
Accepted
time: 0ms
memory: 3740kb
input:
1 2 2 1 2
output:
1 1 2
result:
ok all 1 test passed
Test #3:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
10 5 2 2 2 2 1 2 5 2 2 1 2 1 2 5 2 1 2 2 2 1 5 2 2 1 2 1 1 5 2 1 1 1 2 1 5 2 1 2 2 1 2 5 2 2 1 1 2 2 5 2 2 2 2 1 1 5 2 1 1 2 1 2 5 2 1 2 2 2 1
output:
4 3 4 4 5 2 4 1 4 5 1 2 2 3 3 4 4 5 1 4 4 1 2 4 5 1 3 1 4 5 1 2 2 3 3 4 3 5 1 5 4 3 4 4 5 2 4 1 4 5 1 2 3 4 4 5 1 3 1 5 4 1 2 3 4 1 3 3 5 4 3 4 2 4 1 4 1 5 5 2 3 3 4 4 5 1 3 1 5 4 1 2 4 5 1 3 1 4
result:
ok all 10 test passed
Test #4:
score: 0
Acc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 5 10 2 3 5 6 6 7 7 8 8 9 1 3 1 4 1 5 1 7 1 9 9 2 3 3 4 ...
result:
ok all 10 test passed
Test #6:
score: 0
Accepted
time: 0ms
memory: 5668kb
input:
1 5 2 1 1 2 2 1
output:
4 2 3 4 5 1 3 1 4
result:
ok all 1 test passed
Test #7:
score: 0
Accepted
time: 1ms
memory: 5736kb
input:
1 7 2 2 1 1 2 1 1 2
output:
7 1 2 3 4 4 5 6 7 1 3 4 6 1 6
result:
ok all 1 test passed
Test #8:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
1 9 2 2 1 1 2 1 1 1 2 2
output:
9 1 2 3 4 4 5 7 8 1 3 4 6 4 7 7 9 1 7
result:
ok all 1 test passed
Test #9:
score: 0
Accepted
time: 1ms
memory: 5684kb
input:
4 20 2 2 1 1 2 1 2 1 1 2 1 1 1 1 2 2 2 1 1 2 2 20 2 2 1 2 2 2 1 1 2 2 2 1 2 2 2 2 1 2 2 1 2 20 2 2 2 1 1 2 2 1 1 1 1 1 2 1 1 2 2 1 2 1 1 20 2 2 1 2 2 2 1 2 2 1 1 1 2 1 2 2 1 2 1 1 2
output:
23 1 2 3 4 4 5 5 6 6 7 8 9 9 10 13 14 16 17 18 19 1 3 6 8 9 11 9 12 9 13 13 15 13 16 16 18 18 20 1 5 1 8 1 13 1 18 23 1 2 2 3 5 6 7 8 10 11 11 12 15 16 16 17 18 19 19 20 2 4 2 5 5 7 7 9 7 10 11 13 11 14 11 15 16 18 1 7 1 11 1 16 1 19 23 2 3 4 5 6 7 11 12 12 13 14 15 16 17 17 18 18 19 1 3 1 4 4 6 6 8...
result:
ok all 4 test passed
Test #10:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
4 100 2 2 2 2 1 2 1 1 1 1 2 2 1 2 2 2 1 2 2 2 2 1 1 1 2 2 1 1 2 1 1 2 1 2 2 1 1 1 2 1 2 1 1 2 1 2 1 2 1 2 1 1 1 1 2 1 1 1 2 2 2 1 2 2 2 1 1 1 2 1 2 1 2 2 1 2 2 1 1 2 1 1 1 1 2 2 1 2 1 1 2 2 1 2 2 1 2 1 2 2 2 100 2 2 1 1 1 1 1 2 2 2 1 2 1 2 2 1 2 2 1 1 1 1 2 1 1 1 2 2 1 1 2 1 1 2 1 1 2 1 1 1 1 1 2 1 ...
output:
126 3 4 4 5 5 6 9 10 11 12 12 13 15 16 16 17 20 21 23 24 25 26 27 28 28 29 30 31 31 32 32 33 34 35 37 38 38 39 39 40 40 41 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 53 54 54 55 57 58 60 61 61 62 64 65 67 68 68 69 69 70 70 71 71 72 73 74 74 75 76 77 78 79 79 80 83 84 85 86 86 87 87 88 89 90 91 ...
result:
ok all 4 test passed
Test #11:
score: 0
Accepted
time: 0ms
memory: 5796kb
input:
1 100 2 2 2 1 1 2 2 2 1 1 2 1 1 1 2 2 1 2 2 2 1 1 2 2 1 1 2 2 2 1 2 1 1 2 1 1 2 2 1 2 1 1 2 1 2 2 1 2 2 2 2 1 2 1 2 1 1 2 1 1 1 1 1 2 2 2 1 1 2 1 2 2 2 2 1 1 2 2 2 1 1 2 2 2 1 2 2 1 2 1 2 1 2 2 2 1 2 2 2 1 1
output:
125 2 3 4 5 7 8 9 10 10 11 13 14 15 16 16 17 19 20 21 22 23 24 25 26 28 29 29 30 30 31 32 33 33 34 35 36 37 38 38 39 39 40 41 42 42 43 43 44 45 46 46 47 50 51 51 52 52 53 53 54 54 55 56 57 57 58 62 63 65 66 67 68 68 69 69 70 73 74 75 76 78 79 80 81 83 84 84 85 86 87 87 88 88 89 89 90 90 91 91 92 94 ...
result:
ok all 1 test passed
Test #12:
score: 0
Accepted
time: 0ms
memory: 3800kb
input:
1 100 2 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1 1 2 2 1
output:
123 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1 3 ...
result:
ok all 1 test passed
Test #13:
score: 0
Accepted
time: 1ms
memory: 5676kb
input:
1 200 2 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 ...
output:
208 3 4 13 14 23 24 33 34 43 44 53 54 63 64 73 74 83 84 93 94 103 104 113 114 123 124 133 134 143 144 153 154 163 164 173 174 183 184 193 194 2 4 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 13 15 13 16 13 17 13 18 13 19 13 20 13 21 13 22 13 23 23 25 23 26 23 27 23 28 23 29 23 30 23 31 23 32 23 33 33...
result:
ok all 1 test passed
Test #14:
score: 0
Accepted
time: 0ms
memory: 5788kb
input:
4 7 3 2 2 3 1 3 1 1 7 3 3 1 2 2 3 1 3 7 3 2 1 3 3 2 3 2 7 3 3 2 3 1 3 1 3
output:
9 2 3 3 4 4 5 5 6 1 3 1 4 1 5 1 6 1 7 9 1 2 2 3 4 5 5 6 6 7 1 3 1 4 4 6 1 6 9 1 2 2 3 4 5 5 6 6 7 2 4 2 5 2 6 2 7 10 1 2 2 3 3 4 4 5 5 6 6 7 2 4 2 5 2 6 2 7
result:
ok all 4 test passed
Test #15:
score: -100
Wrong Answer
time: 1ms
memory: 3636kb
input:
4 20 3 1 2 3 1 3 2 1 1 2 3 1 2 2 1 1 2 2 1 2 2 20 3 1 2 1 2 3 3 2 1 1 3 3 2 2 1 2 3 1 1 2 2 20 3 3 1 1 3 3 1 3 2 1 1 2 3 3 3 1 1 2 1 1 1 20 3 3 2 2 1 3 1 1 3 3 2 2 2 1 1 2 1 2 2 2 2
output:
30 1 2 2 3 3 4 4 5 5 6 6 7 8 9 9 10 10 11 11 12 13 14 15 16 17 18 18 19 1 3 4 6 3 6 1 6 6 8 8 10 6 10 1 10 10 12 1 12 1 13 13 15 15 17 18 20 1 17 1 20 31 1 2 2 3 3 4 4 5 6 7 7 8 9 10 11 12 13 14 14 15 15 16 16 17 18 19 3 5 2 5 1 5 1 6 1 7 7 9 7 10 1 10 1 11 1 12 1 13 14 16 13 16 1 16 16 18 16 19 1 1...
result:
wrong answer output = 30, answer = 32.