QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#138330 | #244. Turn Off The Light | nameless_story | 100 ✓ | 168ms | 45968kb | C++20 | 1.8kb | 2023-08-11 15:03:23 | 2023-08-11 15:03:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define N 1200000
const int INF=4000000,mod=1e9+7;
int f1[N][2],g1[N][2],f2[N][2],g2[N][2];
int F(int x,int y){
if (x==0&&y==0) return 0;
if (x==0&&y==1) return INF;
if (x==1&&y==0) return INF;
return 2;
}
int G(int x,int y){
if (x==0&&y==0) return 0;
if (x==0&&y==1) return 3;
if (x==1&&y==0) return 1;
return 2;
}
void solve(){
int n; cin>>n;
string s; cin>>s;
vector<int> a(n+1);
for (int i=1;i<=n;++i) a[i]=s[i-1]-'0';
auto solve=[&](int f[][2],int g[][2]){
for (int j:{0,1}){
if (a[1]==j) f[1][j]=0;
else f[1][j]=INF;
if (a[1]==j) g[1][j]=0;
else g[1][j]=INF;
}
for (int i=2;i<=n;++i){
for (int j:{0,1}){
if (j==a[i]){
f[i][j]=f[i-1][0]+4;
if (f[i-1][0]==0) f[i][j]=0;
}
else{
f[i][j]=f[i-1][1]+2;
}
if (j==a[i]){
g[i][j]=g[i-1][1]+1;
if (g[i-1][0]==0) g[i][j]=0;
}
else{
g[i][j]=min(f[i-1][1]+2,g[i-1][0]+3);
}
}
}
};
solve(f1,g1);
reverse(a.begin()+1,a.begin()+n+1);
solve(f2,g2);
reverse(a.begin()+1,a.begin()+n+1);
long long ans=0;
for (int i=1;i<=n;++i){
int tmp=min(f1[i][a[i]]+g2[n-i+1][0],f1[i][!a[i]]+g2[n-i+1][1]);
tmp=min(tmp,min(f2[n-i+1][a[i]]+g1[i][0],f2[n-i+1][!a[i]]+g1[i][1]));
(ans+=1ll*tmp*i)%=mod;
}
cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int T; cin>>T;
while (T--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 168ms
memory: 45968kb
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