QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#456831 | #244. Turn Off The Light | ClHg2 | 100 ✓ | 303ms | 32576kb | C++14 | 2.1kb | 2024-06-28 14:59:47 | 2024-06-28 14:59:48 |
Judging History
answer
#include <bits/stdc++.h>
#define FOR(i, l, r) for (int i = l; i <= (r); ++i)
#define ROF(i, l, r) for (int i = r; i >= (l); --i)
#define rep(n) FOR (_, 1, n)
#define each(i, x) for (auto& i : x)
using namespace std;
using ll = long long;
using db = long double;
using str = string;
#define mp make_pair
#define f first
#define s second
#define ttt template <typename T
ttt > using V = vector<T>;
ttt, size_t n > using A = array<T, n>;
#define sz(x) int((x).size())
#define bg begin
#define all(x) bg(x), end(x)
#define rsz resize
#define pb push_back
#define eb emplace_back
#define il inline
ttt > il bool ckmin(T& x, const T& y) { return y < x ? x = y, true : false; }
ttt > il bool ckmax(T& x, const T& y) { return x < y ? x = y, true : false; }
constexpr int N = 1e6 + 5, mod = 1e9 + 7;
int n;
str s;
A<int, N> a, b, g;
A<A<int, 2>, N> f;
A<A<int, N>, 2> ans;
void proc() {
cin >> n >> s;
if (s == str(n, '0')) return void(cout << "0\n");
FOR (stage, 0, 1) {
FOR (i, 1, n) a[i] = s[i - 1] - '0';
int l = n, r = 1;
ROF (i, 1, n)
if (a[i]) l = i;
FOR (i, 1, n)
if (a[i]) r = i;
if (l == r) {
ll h = 0;
FOR (i, 1, n) h = (h + (ll)i * (i == l ? 3 : 2 * abs(i - l) - 1)) % mod;
return void(cout << h << "\n");
}
f[r - 1] = {1, 2};
ROF (i, 1, r - 2) {
f[i][0] = 1 + f[i + 1][a[i + 1] ^ 1];
f[i][1] = 3 + f[i + 1][a[i + 1]];
}
g[l] = 0, b[l] = a[l] ^ 1;
FOR (i, l + 1, r - 1) {
if (b[i - 1] == 0)
b[i] = a[i], g[i] = g[i - 1] + 1;
else
b[i] = a[i] ^ 1, g[i] = g[i - 1] + 3;
}
FOR (i, 1, l) ans[stage][i] = f[i][a[i]];
FOR (i, l + 1, r - 1)
ans[stage][i] = i - l + g[i - 1] +
(b[i - 1] == 0 ? 1 + f[i][a[i] ^ 1] : 3 + f[i][a[i]]);
fill(bg(ans[stage]) + r, bg(ans[stage]) + n + 1, 1e9);
reverse(all(s));
}
ll h = 0;
FOR (i, 1, n) h = (h + (ll)i * min(ans[0][i], ans[1][n - i + 1])) % mod;
cout << h << "\n";
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false), cin.exceptions(cin.failbit);
int t;
cin >> t;
rep (t) proc();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 303ms
memory: 32576kb
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