P9575 题解(2023 激励计划评分 8)
Register_int · · 题解
先特判
如果
此时,如果序列中仍有
将
总时间复杂度
AC 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
int n, k, a[MAXN], x[2], y[2], cnt;
int main() {
scanf("%d", &n), k = 20;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), k = min(k, __lg(a[i] & -a[i]));
for (int i = 1; i <= n; i++) a[i] >>= k;
if (~n & 1) {
puts("1");
for (int i = 1; i <= n; i++) putchar(i & 1 ? '1' : '0');
} else {
for (int i = 1; i <= n; i++) cnt += a[i] & 1;
if (cnt & 1) return puts("-1"), 0;
x[0] = y[0] = n - cnt >> 1, x[0]++;
x[1] = y[1] = cnt >> 1; x[1]--, y[1]++;
printf("%d\n", 2 << k);
for (int i = 1; i <= n; i++) putchar(x[a[i] & 1] ? (x[a[i] & 1]--, '0') : '1');
}
}