QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#373254 | #244. Turn Off The Light | SimonLJK | 100 ✓ | 275ms | 24396kb | C++14 | 1011b | 2024-04-01 13:00:12 | 2024-04-01 13:00:14 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+99,mod=1e9+7;
int n,v[N],ans[N],c[N];
int f[N][2];
char ch[N];
void cal(){
int l=n+1,r=0;
for(int i=1;i<=n;i++)
if(v[i])
l=min(i,l),r=max(i,r);
if(r<l){
for(int i=1;i<=n;i++) ans[i]=0;
return;
}
f[r][1]=3; f[r][0]=0;
f[r-1][1]=2; f[r-1][0]=1;
for(int i=r-2;i>=1;i--){
f[i][0]=1+f[i+1][v[i+1]^1];
f[i][1]=3+f[i+1][v[i+1]];
}
int w=0,ptn=0;
for(int i=1;i<=r;i++){
if(i<l) ans[i]=f[i][v[i]];
else ptn^=v[i],ans[i]=f[i][ptn]+w,w+=2+(ptn==0?2:0);
}
for(int i=r+1;i<=n;i++) ans[i]=mod;
return;
}
void sol(){
scanf("%d",&n);
scanf("%s",ch+1);
for(int i=1;i<=n;i++)
v[i]=ch[i]-'0';
cal();
for(int i=1;i<=n;i++)
c[i]=ans[i];
reverse(v+1,v+n+1);
cal();
reverse(ans+1,ans+n+1);
int re=0;
for(int i=1;i<=n;i++){
c[i]=min(c[i],ans[i]);
re=(long long)(re+1ll*i*c[i])%mod;
}
printf("%d\n",re);
}
int main(){
int T; scanf("%d",&T);
while(T--) sol();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 275ms
memory: 24396kb
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