QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#371445 | #244. Turn Off The Light | zlt | 100 ✓ | 336ms | 35564kb | C++14 | 3.8kb | 2024-03-30 12:23:21 | 2024-03-30 12:23:22 |
Judging History
answer
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 1000100;
const ll mod = 1000000007;
int n, a[maxn], b[maxn], tag[maxn];
ll f[maxn], g[maxn];
char s[maxn];
void solve() {
scanf("%d%s", &n, s + 1);
for (int i = 0; i <= n + 1; ++i) {
tag[i] = 0;
f[i] = g[i] = 1e18;
}
bool fl = 1;
for (int i = 1; i <= n; ++i) {
a[i] = s[i] - '0';
fl &= (!a[i]);
}
if (fl) {
puts("0");
return;
}
vector<int> S;
for (int i = 1; i <= n; ++i) {
if (a[i]) {
S.pb(i);
}
b[i] = a[i];
if (i > 1) {
b[i] ^= (b[i - 1] ^ 1);
}
}
int c0 = 0, c1 = 0;
for (int i = 1; i <= S.back(); ++i) {
c0 += (b[i] == 0);
c1 += (b[i] == 1);
}
for (int i = 1; i <= S[0]; ++i) {
f[i] = c0 + c1 * 3;
tag[i] ^= tag[i - 1];
if (i + 1 <= S.back() && (b[S.back()] ^ tag[i]) && (b[S.back() - 1] ^ tag[i])) {
f[i] -= 4;
}
if (!(b[S.back()] ^ tag[i])) {
--f[i];
}
// printf("%d %d %d %lld\n", i, c0, c1, f[i]);
c0 -= ((b[i] ^ tag[i]) == 0);
c1 -= ((b[i] ^ tag[i]) == 1);
tag[i] ^= 1;
swap(c0, c1);
}
for (int i = 0; i <= n + 1; ++i) {
tag[i] = 0;
}
c0 = c1 = 0;
int s0 = 0, s1 = 0;
for (int i = S[0]; i <= S.back(); ++i) {
b[i] = a[i];
if (i > S[0]) {
b[i] ^= (b[i - 1] ^ 1);
} else {
b[i] ^= 1;
}
if (i > S[0]) {
c0 += (b[i] == 0);
c1 += (b[i] == 1);
} else {
s0 += (b[i] == 0);
s1 += (b[i] == 1);
}
}
for (int i = S[0] + 1; i < S.back(); ++i) {
tag[i] ^= tag[i - 1];
f[i] = (c0 + s0) + (c1 + s1) * 3 + i - S[0];
if ((b[S.back()] ^ tag[i]) && (b[S.back() - 1] ^ tag[i])) {
f[i] -= 4;
}
if (!(b[S.back()] ^ tag[i])) {
--f[i];
}
// printf("%d %d %d %d %d %lld\n", i, c0, s0, c1, s1, f[i]);
c0 -= ((b[i] ^ tag[i]) == 0);
c1 -= ((b[i] ^ tag[i]) == 1);
tag[i] ^= 1;
swap(c0, c1);
s0 += ((b[i] ^ tag[i]) == 0);
s1 += ((b[i] ^ tag[i]) == 1);
}
vector<int>().swap(S);
for (int i = 0; i <= n + 1; ++i) {
tag[i] = 0;
}
reverse(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i) {
if (a[i]) {
S.pb(i);
}
b[i] = a[i];
if (i > 1) {
b[i] ^= (b[i - 1] ^ 1);
}
}
c0 = c1 = 0;
for (int i = 1; i <= S.back(); ++i) {
c0 += (b[i] == 0);
c1 += (b[i] == 1);
}
for (int i = 1; i <= S[0]; ++i) {
g[i] = c0 + c1 * 3;
tag[i] ^= tag[i - 1];
if (i + 1 <= S.back() && (b[S.back()] ^ tag[i]) && (b[S.back() - 1] ^ tag[i])) {
g[i] -= 4;
}
if (!(b[S.back()] ^ tag[i])) {
--g[i];
}
c0 -= ((b[i] ^ tag[i]) == 0);
c1 -= ((b[i] ^ tag[i]) == 1);
tag[i] ^= 1;
swap(c0, c1);
}
for (int i = 0; i <= n + 1; ++i) {
tag[i] = 0;
}
c0 = c1 = s0 = s1 = 0;
for (int i = S[0]; i <= S.back(); ++i) {
b[i] = a[i];
if (i > S[0]) {
b[i] ^= (b[i - 1] ^ 1);
} else {
b[i] ^= 1;
}
if (i > S[0]) {
c0 += (b[i] == 0);
c1 += (b[i] == 1);
} else {
s0 += (b[i] == 0);
s1 += (b[i] == 1);
}
}
for (int i = S[0] + 1; i < S.back(); ++i) {
tag[i] ^= tag[i - 1];
g[i] = (c0 + s0) + (c1 + s1) * 3 + i - S[0];
if ((b[S.back()] ^ tag[i]) && (b[S.back() - 1] ^ tag[i])) {
g[i] -= 4;
}
if (!(b[S.back()] ^ tag[i])) {
--g[i];
}
c0 -= ((b[i] ^ tag[i]) == 0);
c1 -= ((b[i] ^ tag[i]) == 1);
tag[i] ^= 1;
swap(c0, c1);
s0 += ((b[i] ^ tag[i]) == 0);
s1 += ((b[i] ^ tag[i]) == 1);
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
f[i] = min(f[i], g[n - i + 1]);
ans = (ans + f[i] * i) % mod;
}
printf("%lld\n", ans);
}
int main() {
int T = 1;
scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 336ms
memory: 35564kb
input:
146645 2 00 2 10 2 01 2 11 3 000 3 100 3 010 3 110 3 001 3 101 3 011 3 111 4 0000 4 1000 4 0100 4 1100 4 0010 4 1010 4 0110 4 1110 4 0001 4 1001 4 0101 4 1101 4 0011 4 1011 4 0111 4 1111 5 00000 5 10000 5 01000 5 11000 5 00100 5 10100 5 01100 5 11100 5 00010 5 10010 5 01010 5 11010 5 00110 5 10110 5...
output:
0 5 7 6 0 14 10 12 14 24 12 26 0 34 22 36 18 40 20 38 26 60 40 58 24 62 42 60 0 69 47 66 33 80 50 83 31 90 60 93 34 87 57 80 45 120 90 123 64 117 87 110 42 123 93 120 67 118 88 129 0 123 89 126 63 128 86 125 49 150 108 147 70 153 111 152 51 168 126 165 88 171 129 170 54 165 123 168 85 154 112 159 73...
result:
ok 146645 lines