QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#371784#244. Turn Off The Lightwsyear100 ✓260ms32012kbC++142.3kb2024-03-30 16:13:012024-03-30 16:13:02

Judging History

This is the latest submission verdict.

  • [2024-03-30 16:13:02]
  • Judged
  • Verdict: 100
  • Time: 260ms
  • Memory: 32012kb
  • [2024-03-30 16:13:01]
  • Submitted

answer

#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T> void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T> void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

const int maxn = 1000010;
const int mod = 1000000007;

inline void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
inline void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
inline int Add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; }
inline int Sub(int x, int y) { x -= y; if (x < 0) x += mod; return x; }

int n, a[maxn], b[maxn], f[maxn], g[maxn], c[maxn], cnt0[maxn], cnt1[maxn];
char s[maxn];

void calc(int *ans) {
  rep (i, 1, n) b[i] = a[i], ans[i] = 1e9;
  int s = n + 1, t = 0;
  rep (i, 1, n) if (b[i]) chkmn(s, i), chkmx(t, i);
  c[s - 1] = 0;
  rep (i, s, t) c[i] = c[i - 1] ^ (!b[i]);
  cnt0[s - 1] = cnt1[s - 1] = 0;
  rep (i, s, t) cnt0[i] = cnt0[i - 1] + ((c[i - 1] ^ b[i]) == 0);
  rep (i, s, t) cnt1[i] = cnt1[i - 1] + ((c[i - 1] ^ b[i]) == 1);
  rep (i, 1, s - 1) {
    int ps = s - i;
    ans[i] = ps + ps / 2 * 2;
    if (ps & 1) ans[i] += 3 * cnt0[t - 1] + cnt1[t - 1];
    else ans[i] += 3 * cnt1[t - 1] + cnt0[t - 1];
    if ((ps & 1) ^ c[t - 1] ^ b[t]) ans[i]--;
  }
  int r0 = cnt0[t - 1], r1 = cnt1[t - 1], s0 = 0, s1 = 0, sum = 0, lz = 0;
  rep (i, s, t - 1) sum ^= (!b[i]);
  rep (i, s, t - 1) {
    ans[i] = 3 * (r1 + s1) + r0 + s0 + i - s;
    if (b[t] ^ c[t - 1] ^ lz) ans[i]--;
    if ((b[i] ^ c[i - 1] ^ lz)) r1--, s0++;
    else r0--, s1++;
    swap(r0, r1), lz ^= 1;
  }
  if (s == t) ans[s] = 3;
}

void work() {
  cin >> n >> s;
  rep (i, 1, n) a[i] = s[i - 1] - '0';
  int s1 = 0;
  rep (i, 1, n) s1 += a[i];
  if (!s1) return cout << "0\n", void();
  calc(f), reverse(a + 1, a + n + 1), calc(g);
  int sum = 0;
  rep (i, 1, n) add(sum, 1ll * i * min(f[i], g[n - i + 1]) % mod);
  cout << sum << '\n';
}

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  int t; cin >> t;
  while (t--) work();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 260ms
memory: 32012kb

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