QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#373339#244. Turn Off The LightAak100 ✓214ms19888kbC++142.0kb2024-04-01 14:52:372024-04-01 14:52:37

Judging History

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

  • [2024-04-01 14:52:37]
  • 评测
  • 测评结果:100
  • 用时:214ms
  • 内存:19888kb
  • [2024-04-01 14:52:37]
  • 提交

answer

#include <bits/stdc++.h>

#define rep(i, a, b) for(int i = (a), i##end = (b); i <= i##end; i++)
#define _rep(i, a, b) for(int i = (a), i##end = (b); i >= i##end; i--)
#define ec first
#define fb second
#define dl make_pair
#define dk(...) make_tuple(__VA_ARGS__)

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

int read() {
	int x = 0, f = 1; char c = getchar();
	while (!isdigit(c)) {
		if (c == '-') f = -1;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	return x * f;
}

template <typename _Tp>
void print(_Tp x) {
	if (x < 0) x = (~x + 1), putchar('-');
	if (x > 9) print(x / 10);
	putchar(x % 10 + '0');
}

const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 5;
int T, n, ans[MAXN];
string s;
int ch[MAXN];
int f[MAXN][2];

void calc() {
	int flg = 0;
	rep (i, 1, n) {
		if (!flg) f[i][ch[i] ^ 1] = 1, f[i][ch[i]] = 1e9;
		else f[i][ch[i]] = f[i - 1][0] + 2, f[i][ch[i] ^ 1] = f[i - 1][1] + 4;
		flg += ch[i];
	}
	flg = 0;
	int g0 = 0, g1 = 3;
	_rep (i, n, 1) {
		int nu = 1 + (ch[i] ? g0 : g1), nv = 3 + (ch[i] ? g1 : g0);
		ans[i] = min(ans[i], min(g0 + f[i][0], g1 + f[i][1])) % MOD;
		flg += ch[i];
		g0 = nu, g1 = nv;
		if (!flg) g0 = 0, g1 = 3;
		if (flg == 1 && ch[i] == 1) g1 = 2;
	}
}

void solve() {
	cin >> n;
	cin >> s;
	bool flg = 0;
	for (auto c : s) flg |= (c ^ 48);
	if (!flg) return puts("0"), void();
	rep (i, 1, n) ch[i] = s[i - 1] - '0';
	rep (i, 0, n + 1) ans[i] = 1e9;
	calc();
	reverse(ch + 1, ch + 1 + n);
	reverse(ans + 1, ans + 1 + n);
	calc();
	reverse(ch + 1, ch + 1 + n);
	reverse(ans + 1, ans + 1 + n);
	int fans = 0;
	rep (i, 1, n) fans = (fans + 1ll * i * min(ans[i - 1], ans[i + 1])) % MOD;
	print(fans), puts("");
}

signed main() {
#ifndef LOCAL
#ifndef ONLINE_JUDGE
	freopen("light.in", "r", stdin);
	freopen("light.out", "w", stdout);
#endif
#endif
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> T;
	while (T --> 0) solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 214ms
memory: 19888kb

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