QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#371446 | #244. Turn Off The Light | JWRuixi | 100 ✓ | 149ms | 24228kb | C++17 | 2.1kb | 2024-03-30 12:23:49 | 2024-03-30 12:23:49 |
Judging History
answer
#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
using namespace std;
using vi = vector<int>;
constexpr int N = 1e6 + 9;
constexpr int mod = 1e9 + 7;
int n, a[N], f[N][2], g[N], as[N];
char s[N];
IL void cmin (int &x, const int &y) {
x > y && (x = y, 0);
}
void slv () {
cin >> n >> (s + 1);
if (count(s + 1, s + n + 1, '1') == 0) {
cout << "0\n";
return;
}
if (count(s + 1, s + n + 1, '1') == 1) {
int p = find(s + 1, s + n + 1, '1') - s;
int z = 0;
L (i, 1, p - 1) {
z = (z + (LL)i * (2 * (p - i) - 1)) % mod;
}
z = (z + 3LL * p) % mod;
L (i, p + 1, n) {
z = (z + (LL)i * (2 * (i - p) - 1)) % mod;
}
cout << z << '\n';
return;
}
L (i, 1, n) {
a[i] = s[i] ^ 48;
}
memset(as, 0x3f, (n + 1) << 2);
L (o, 0, 1) {
int L, R;
L (i, 1, n) {
if (a[i]) {
L = i;
break;
}
}
R (i, n, 1) {
if (a[i]) {
R = i;
break;
}
}
L (i, R + 1, n + 1) {
f[i][0] = f[i][1] = 0;
}
f[R][0] = a[R] ? 0 : 3;
f[R][1] = a[R] ? 1 : 2;
R (i, R - 1, 1) {
f[i][0] = f[i + 1][a[i] ^ 1];
f[i][1] = f[i + 1][a[i]] + 2;
}
L (i, 0, L) {
g[i] = 0;
}
int C = 0;
L (i, L + 1, n) {
C += a[i - 1];
g[i] = g[i - 1] + (C % 2 ? 0 : 2);
}
C = 0;
L (p, 1, n) {
C += a[p - 1];
int l = min(p, L);
int r = max(p, R);
cmin(as[o ? n - p + 1 : p], g[p - 1] + (p > L ? f[p][C % 2 == 0] : f[p + 1][a[p]]) + p - l + r - l);
}
reverse(a + 1, a + n + 1);
}
int z = 0;
L (i, 1, n) {
z = (z + (LL)as[i] * i) % mod;
}
cout << z << '\n';
}
int main () {
// FIO("light");
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
slv();
}
}
// I love WHQ!
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 149ms
memory: 24228kb
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