QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#455507 | #244. Turn Off The Light | December456 | 100 ✓ | 229ms | 37732kb | C++14 | 3.2kb | 2024-06-26 15:36:09 | 2024-06-26 15:36:10 |
Judging History
answer
#include <algorithm>
#include <cstdio>
constexpr int MAXN = 1000000 + 2;
constexpr int P = 1000000007;
int pre[MAXN][2], suf[MAXN][2];
int pre_rev[MAXN][2], suf_rev[MAXN][2];
int sum[MAXN];
char s[MAXN];
inline void add(int &x, int y) {
(x += y) >= P && (x -= P);
}
void solve() {
int n, L, R, ans = 0;
scanf("%d%s", &n, s + 1);
for (L = 1; L <= n && s[L] == '0'; L ++);
for (R = n; R && s[R] == '0'; R --);
if (!R) {
puts("0");
return;
}
if (L == R) {
for (int i = 1; i <= n; i ++) {
if (i == L) {
add(ans, 3 * L);
} else {
add(ans, ((abs(L - i) << 1) - 1ll) * i % P);
}
}
printf("%d\n", ans);
return;
}
for (int i = 1; i <= n; i ++) {
sum[i] = sum[i - 1] + (s[i] ^ '0');
}
bool fl, tag;
for (int i = 0; i < 2; i ++) {
pre[L][i] = pre_rev[L][i] = 0;
}
for (int i = L + 1; i <= n; i ++) {
fl = s[i] ^ '0' ^ 1;
for (int j = 0; j < 2; j ++) {
pre[i][j] = pre[i - 1][j ^ fl] + (j == fl);
pre_rev[i][j] = pre_rev[i - 1][j];
}
pre_rev[i][(sum[i] - sum[L]) & 1] ++;
}
for (int i = 0; i < 2; i ++) {
suf[R][i] = suf_rev[R][i] = 0;
}
for (int i = R - 1; i; i --) {
fl = s[i] ^ '0' ^ 1;
for (int j = 0; j < 2; j ++) {
suf[i][j] = suf[i + 1][j ^ fl] + (j == fl);
suf_rev[i][j] = suf_rev[i + 1][j];
}
suf_rev[i][(sum[R - 1] - sum[i - 1]) & 1] ++;
}
for (int i = 1; i < L; i ++) {
tag = (R ^ i ^ sum[R] ^ sum[i] ^ 1) & 1;
add(ans, i * (suf[i + 1][0] + 3ll * suf[i + 1][1] + tag) % P);
}
for (int i = R + 1; i <= n; i ++) {
tag = (i ^ L ^ sum[i - 1] ^ sum[L]) & 1;
add(ans, i * (pre[i - 1][0] + 3ll * pre[i - 1][1] + tag) % P);
}
if (L + 1 == R) {
add(ans, (L + R) << 1);
printf("%d\n", ans);
return;
}
for (int i = L + 1; i < R; i ++) {
fl = (sum[i - 1] ^ sum[L]) & 1;
tag = (R ^ i ^ sum[R] ^ sum[i - 1] ^ fl) & 1;
int cost = i - L + pre_rev[i - 1][0] + suf[i][fl] + tag
+ 3 * (pre_rev[i - 1][1] + suf[i][fl ^ 1]);
fl = (sum[R - 1] ^ sum[i]) & 1;
tag = (i ^ L ^ sum[i] ^ sum[L] ^ fl ^ 1) & 1;
cost = std::min(cost, R - i + suf_rev[i + 1][0] + tag
+ pre[i][fl] + 3 * (suf_rev[i + 1][1] + pre[i][fl ^ 1]));
add(ans, (long long){i} * cost % P);
}
fl = s[L + 1] & 1;
tag = (R ^ L ^ sum[R] ^ sum[L + 1] ^ fl) & 1;
add(ans, L * (2ll + pre_rev[L + 1][0] + suf[L + 2][fl] + tag
+ 3 * (pre_rev[L + 1][1] + suf[L + 2][fl ^ 1])));
fl = s[R - 1] & 1;
tag = (R ^ L ^ sum[R - 2] ^ sum[L] ^ fl ^ 1) & 1;
add(ans, R * (2ll + suf_rev[R - 1][0] + pre[R - 2][fl] + tag
+ 3 * (suf_rev[R - 1][1] + pre[R - 2][fl ^ 1])) % P);
printf("%d\n", ans);
}
int main() {
int T;
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: 229ms
memory: 37732kb
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