This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#define N 1000000
int check(char *cc, int n) {
int i, j, d;
d = 2;
for (i = 0; i + 1 < n; i++)
if (cc[i] != cc[i + 1])
d++;
for (i = 0; i < n; i = j) {
j = i + 1;
while (j < n && cc[j] != cc[j - 1])
j++;
if (j - i > (d + 1) / 2)
return 0;
}
if (d % 2 == 0)
return 1;
i = 0;
while (i + 1 < n && cc[i] != cc[i + 1])
i++;
if (i == n - 1)
return 0;
j = n - 1;
while (j > 0 && cc[j] != cc[j - 1])
j--;
if (j == 0)
return 0;
return (i + 1) + (n - j) <= d / 2 + 1;
}
int ii[N], ll[N], m;
void remove_(int i, int l) {
while (l > 0)
if (l % 2 != 0)
ii[m] = i, ll[m] = 3, m++, l -= 3;
else
ii[m] = i, ll[m] = 2, m++, l -= 2;
}
int solve(char *cc, int n, int t, int print) {
static char cc_[N + 1];
int n_, nl, nr, h, i, i_, j, j_, d, d_;
d = 2;
for (i = 0; i + 1 < n; i++)
if (cc[i] != cc[i + 1])
d++;
n_ = 0, d_ = 0, m = 0;
for (i = 0; i <= n; i++) {
if (i == n || n_ > 0 && cc[i] != cc_[n_ - 1])
if (n_ >= 2 && cc_[n_ - 2] == cc_[n_ - 1] && (d % 2 != 0 || d_ > 1 && i < n) && t > 0) {
i_ = n_ - 3;
while (i_ >= 0 && cc_[i_] == cc_[n_ - 1])
i_--;
i_++;
if (print)
remove_(i_, n_ - i_);
n_ = i_;
t--;
if (--d_ == 0 || i == n)
d--;
}
if (i < n) {
if (n_ == 0 || cc_[n_ - 1] != cc[i])
d_++;
cc_[n_++] = cc[i];
}
}
d = 2;
for (i = 0; i + 1 < n_; i++)
if (cc_[i] != cc_[i + 1])
d++;
if (print) {
cc_[n_] = 0;
i_ = j_ = -1;
for (i = 0; i < n_; i = j) {
j = i + 1;
while (j < n_ && cc_[j] != cc_[j - 1])
j++;
if (j - i == (d + 1) / 2) {
i_ = i, j_ = j - 1;
break;
}
}
nl = 0;
for (i = j_ + 1; i < n_; i = j) {
j = i + 1;
while (j < n_ && cc_[j] == cc_[i])
j++;
if (j < n_)
remove_(j_--, j - i + 1);
else
nl = j - i;
}
nr = 0;
for (j = i_ - 1; j >= 0; j = i) {
i = j - 1;
while (i >= 0 && cc_[i] == cc_[j])
i--;
if (i >= 0)
remove_(i + 1, j - i + 1);
else
nr = j - i;
}
if (d % 2 == 0)
remove_(0, nl + nr + 1);
else
remove_(0, nl + 1), remove_(0, nr + 1);
printf("%d\n", m);
for (h = 0; h < m; h++)
printf("%d %d\n", ii[h] + 1, ll[h]);
}
return check(cc_, n_);
}
int main() {
static char cc[N + 1];
int n, t, lower, upper;
scanf("%d%s", &n, cc);
if (!check(cc, n)) {
printf("-1\n");
return 0;
}
lower = 0, upper = n + 1;
while (upper - lower > 1) {
t = (lower + upper) / 2;
if (solve(cc, n, t, 0))
lower = t;
else
upper = t;
}
solve(cc, n, lower, 1);
return 0;
}
Compilation message (stderr)
Main.c: In function 'solve':
Main.c:54:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
54 | if (i == n || n_ > 0 && cc[i] != cc_[n_ - 1])
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.c:55:71: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
55 | if (n_ >= 2 && cc_[n_ - 2] == cc_[n_ - 1] && (d % 2 != 0 || d_ > 1 && i < n) && t > 0) {
| ~~~~~~~^~~~~~~~
Main.c: In function 'main':
Main.c:124:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |