QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#396942 | #244. Turn Off The Light | yz_ly | 100 ✓ | 253ms | 28264kb | C++14 | 1.7kb | 2024-04-23 14:42:13 | 2024-04-23 14:42:13 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
char ch=getchar();
int f=1,x=0;
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-f;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
return f*x;
}
inline void work(ll k){
if(k<0){
putchar('-');
k=-k;
}
if(k>9)
work(k/10);
putchar(k%10+'0');
}
/*
策略很简单,走到最边上在走回去
dp维护不取反的一边,另一边动态维护
但是大模拟
*/
const ll mod=1e9+7;
int t,n;
ll ans[1000005],dp[1000005][2];
char s[1000005];
void solve(){
int l=0,r=0;
for(int i=1;i<=n;i++){
if(s[i]=='1'){
l=i;
break;
}
}
for(int i=n;i;i--){
if(s[i]=='1'){
r=i;
break;
}
}
dp[r-1][1]=2;
dp[r-1][0]=1;
for(int i=r-2;i>0;i--){
dp[i][0]=dp[i+1][!(s[i+1]-'0')]+1;
dp[i][1]=dp[i+1][s[i+1]-'0']+3;
}
for(int i=1,now=0,f=0;i<r;i++){
if(i<l)
ans[i]=min(dp[i][s[i]-'0'],ans[i]);
else{
ans[i]=min(ans[i],dp[i][f^(s[i]-'0')]+now);
if((s[i]=='1')^f){
f=1;
now++;
}
else{
now+=3;
f=0;
}
now++;
}
}
}
int main(){
t=read();
while(t--){
n=read();
scanf("%s",s+1);
int flag=0,num=0;
for(int i=1;i<=n;i++){
ans[i]=1e9;
if(s[i]=='1')
flag=1,num++;
}
if(!flag){
puts("0");
continue;
}
if(num==1){
int id=0;
for(int i=1;i<=n;i++){
if(s[i]=='1')
id=i;
}
ans[id]=3;
}
solve();
reverse(s+1,s+n+1);
reverse(ans+1,ans+n+1);
solve();
reverse(ans+1,ans+n+1);
ll sum=0;
for(int i=1;i<=n;i++){
sum+=ans[i]*i%mod;
sum%=mod;
}
work(sum);
putchar('\n');
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 253ms
memory: 28264kb
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