QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#371531 | #244. Turn Off The Light | 08kevin | 100 ✓ | 294ms | 106404kb | C++14 | 2.1kb | 2024-03-30 13:46:39 | 2024-03-30 13:46:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll T, n, ans;
ll a[1000100];
ll b[1000100];
ll d[1000100];
ll f[1000100][4];
ll g[1000100][4];
ll p[1000100];
ll q[1000100];
char c[1000100];
inline void work1() {
f[1][0] = 0;
f[1][1] = 3;
for (ll i = 2; i <= n; i++) {
if (!f[i - 1][0]) {
if (!a[i - 1]) {
f[i][0] = 0;
f[i][1] = 3;
} else {
f[i][0] = 1;
f[i][1] = 2;
}
} else {
if (!a[i - 1]) {
f[i][0] = f[i - 1][1] + 1;
f[i][1] = 3 + f[i - 1][0];
} else {
f[i][0] = f[i - 1][0] + 1;
f[i][1] = 3 + f[i - 1][1];
}
}
}
g[n][0] = 0;
g[n][1] = 3;
for (ll i = n - 1; i >= 1; i--) {
if (!g[i + 1][0]) {
if (!a[i + 1]) {
g[i][0] = 0;
g[i][1] = 3;
} else {
g[i][0] = 1;
g[i][1] = 2;
}
} else {
if (!a[i + 1]) {
g[i][0] = g[i + 1][1] + 1;
g[i][1] = 3 + g[i + 1][0];
} else {
g[i][0] = g[i + 1][0] + 1;
g[i][1] = 3 + g[i + 1][1];
}
}
}
return;
}
int main() {
// freopen("light.in", "r", stdin);
// freopen("light.out", "w", stdout);
scanf("%lld", &T);
while (T--) {
ans = 0;
scanf("%lld", &n);
scanf("%s", c + 1);
for (ll i = 1; i <= n; i++) {
a[i] = c[i] - '0';
b[i] = d[i] = a[i];
}
work1();
p[1] = 0;
for (ll i = 2; i <= n; i++) {
if (!a[i - 1] && !p[i - 1]) {
p[i] = 0;
continue;
}
p[i] = p[i - 1] + 2;
if (!b[i - 1]) {
p[i] += 2;
b[i] = a[i];
} else {
b[i] = (a[i] ^ 1);
}
}
q[n] = 0;
for (ll i = n - 1; i >= 1; i--) {
if (!a[i + 1] && !q[i + 1]) {
q[i] = 0;
continue;
}
q[i] = q[i + 1] + 2;
if (!d[i + 1]) {
q[i] += 2;
d[i] = a[i];
} else {
d[i] = (a[i] ^ 1);
}
}
for (ll i = 1; i <= n; i++) {
ans += i * min(p[i] + g[i][b[i]], q[i] + f[i][d[i]]);
ans %= mod;
}
printf("%lld\n", ans);
for (ll i = 1; i <= n; i++) {
f[i][0] = g[i][0] = 0;
f[i][1] = g[i][1] = 0;
p[i] = q[i] = 0;
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 294ms
memory: 106404kb
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