QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#371783#244. Turn Off The Lightwsyear0 267ms31992kbC++142.2kb2024-03-30 16:10:442024-03-30 16:10:44

Judging History

你现在查看的是最新测评结果

  • [2024-03-30 16:10:44]
  • 评测
  • 测评结果:0
  • 用时:267ms
  • 内存:31992kb
  • [2024-03-30 16:10:44]
  • 提交

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;
  }
}

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: 0
Wrong Answer
time: 267ms
memory: 31992kb

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
1000000002
999999994
6
0
4
999999997
12
999999991
24
12
26
0
24
2
36
999999995
40
20
38
999999993
60
40
58
24
62
42
60
0
59
27
66
3
80
50
83
999999998
90
60
93
34
87
57
80
1000000002
120
90
123
64
117
87
110
42
123
93
120
67
118
88
129
0
113
69
126
33
128
86
125
9
150
108
147
70
153
111
152
1
168
...

result:

wrong answer 2nd lines differ - expected: '5', found: '1000000002'