QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#371634#244. Turn Off The Lightyyyyxh100 ✓155ms26984kbC++141.4kb2024-03-30 14:37:112024-03-30 14:37:21

Judging History

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

  • [2024-03-30 14:37:21]
  • 评测
  • 测评结果:100
  • 用时:155ms
  • 内存:26984kb
  • [2024-03-30 14:37:11]
  • 提交

answer

#include <cstdio>
#include <algorithm>
using namespace std;
int read(){
	char c=getchar();int x=0;
	while(c<48||c>57) c=getchar();
	do x=x*10+(c^48),c=getchar();
	while(c>=48&&c<=57);
	return x;
}
const int N=1000003;
int n;
bool s[N],par,a[N];
int arr[N],res[N];
int pre[N],suf[N],tmp[N],mn[N];
inline void chmn(int &x,int v){(x>v)&&(x=v);}
void sol(){
	a[0]=0;
	for(int i=1;i<=n;++i) a[i]=s[i]^a[i-1];
	pre[1]=suf[n]=0;
	int pos=1;
	while(pos<=n&&!s[pos]) pre[++pos]=0;
	for(int i=pos;i<n;++i) pre[i+1]=pre[i]+(!a[i])*2+2;
	pos=n;
	while(pos&&!s[pos]) suf[--pos]=0;
	for(int i=pos;i>1;--i) suf[i-1]=suf[i]+(!(a[i-1]^par))*2+2;
	tmp[1]=tmp[2]=0;
	for(int i=1;i+2<=n;++i) tmp[i+2]=tmp[i]+(a[i]!=par)*2+(a[i+1]==par)*2+2;
	mn[n+1]=mn[n+2]=0x3f3f3f3f;
	for(int i=n;i;--i) chmn(mn[i]=suf[i]+tmp[i],mn[i+2]);
	for(int i=1;i+par<=n;++i){
		arr[i]=pre[i]-tmp[i+par]+mn[i+par];
		if(par){
			if(a[i]) arr[i]+=3;
			else arr[i]++;
		}
	}
	if(par) arr[n]=0x3f3f3f3f;
}
void solve(){
	n=read();par=0;
	char cc=getchar();
	while(cc<48||cc>49) cc=getchar();
	for(int i=1;i<=n;++i){s[i]=(cc=='1');par^=s[i];cc=getchar();}
	sol();
	for(int i=1;i<=n;++i) res[i]=arr[i];
	reverse(s+1,s+n+1);
	sol();
	for(int i=1;i<=n;++i) if(res[i]>arr[n-i+1]) res[i]=arr[n-i+1];
	__int128 sum=0;
	for(int i=1;i<=n;++i) sum+=(long long)i*res[i];
	printf("%d\n",int(sum%1000000007));
}
int main(){
	int tc=read();
	while(tc--) solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 155ms
memory: 26984kb

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