QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#371759#244. Turn Off The Lightqwqwf100 ✓237ms24128kbC++141.2kb2024-03-30 15:42:232024-03-30 15:42:25

Judging History

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

  • [2024-03-30 15:42:25]
  • 评测
  • 测评结果:100
  • 用时:237ms
  • 内存:24128kb
  • [2024-03-30 15:42:23]
  • 提交

answer

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#pragma GCC optimize("Ofast","unroll-loops","inline")
#include<bits/stdc++.h>
#define ll long long
//#define int ll
#define pb push_back
using namespace std;
const int N=1e6+10,M=1e6+10,mod=1e9+7;
inline void Add(int &x,int y){x+=y;if(x>=mod) x-=mod;}
const int Inf=1e9;
int f[N][2],a[N],n;
void calc(int *ans){
	int B=0,E=0;
	for(int i=1;i<=n;i++) if(a[i]){
		if(!B) B=i;E=i;
	}
	f[E][1]=2,f[E][0]=1;
	for(int i=E-1;i;i--) for(int j=0;j<2;j++){
		f[i][j]=f[i+1][j^a[i]^1]+2*j+1;
	}
	for(int i=1,w=0,lst=0;i<E;i++){
		if(i<=B) ans[i]=f[i+1][a[i]];
		else ans[i]=(i-B)+w+f[i][lst],w+=(lst?3:1),lst^=a[i];
	}
	for(int i=E;i<=n;i++) ans[i]=Inf;
	if(B==E) ans[E]=3;
}
int ans[2][N];
char s[N];
void solve(){
	cin>>n>>(s+1);
	int Ans=0;
	for(int i=1;i<=n;i++) a[i]=s[i]=='1';
	bool flag=0;for(int i=1;i<=n;i++) flag|=a[i];
	if(!flag) return cout<<0<<'\n',void();
	calc(ans[0]);reverse(a+1,a+n+1);calc(ans[1]);
	for(int i=1;i<=n;i++) Add(Ans,1ll*min(ans[0][i],ans[1][n-i+1])*i%mod);
	cout<<Ans<<'\n';
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int T;cin>>T;
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 237ms
memory: 24128kb

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